PlayaEpetraVector.cpp
Go to the documentation of this file.
00001 /* @HEADER@ */
00002 // ************************************************************************
00003 // 
00004 //                 Playa: Programmable Linear Algebra
00005 //                 Copyright 2012 Sandia Corporation
00006 // 
00007 // Under the terms of Contract DE-AC04-94AL85000 with Sandia Corporation,
00008 // the U.S. Government retains certain rights in this software.
00009 //
00010 // Redistribution and use in source and binary forms, with or without
00011 // modification, are permitted provided that the following conditions are
00012 // met:
00013 //
00014 // 1. Redistributions of source code must retain the above copyright
00015 // notice, this list of conditions and the following disclaimer.
00016 //
00017 // 2. Redistributions in binary form must reproduce the above copyright
00018 // notice, this list of conditions and the following disclaimer in the
00019 // documentation and/or other materials provided with the distribution.
00020 //
00021 // 3. Neither the name of the Corporation nor the names of the
00022 // contributors may be used to endorse or promote products derived from
00023 // this software without specific prior written permission.
00024 //
00025 // THIS SOFTWARE IS PROVIDED BY SANDIA CORPORATION "AS IS" AND ANY
00026 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00027 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
00028 // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SANDIA CORPORATION OR THE
00029 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
00030 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
00031 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
00032 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
00033 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
00034 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
00035 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00036 //
00037 // Questions? Contact Kevin Long (kevin.long@ttu.edu)
00038 // 
00039 
00040 /* @HEADER@ */
00041 
00042 #include "PlayaEpetraVector.hpp"
00043 #include "PlayaEpetraVectorSpace.hpp"
00044 #include "Teuchos_Assert.hpp"
00045 
00046 #ifndef HAVE_TEUCHOS_EXPLICIT_INSTANTIATION
00047 #include "PlayaVectorImpl.hpp"
00048 #include "PlayaLinearOperatorImpl.hpp"
00049 #endif
00050 
00051 using Teuchos::RCP;
00052 using namespace Playa;
00053 
00054 EpetraVector::EpetraVector(const VectorSpace<double>& vs)
00055   : SingleChunkVector<double>(), 
00056     vecSpace_(vs),
00057     epetraVec_(), 
00058     numLocalElements_(vs.numLocalElements())
00059 {
00060   const EpetraVectorSpace* epvs 
00061     = dynamic_cast<const EpetraVectorSpace*>(vs.ptr().get());
00062   TEUCHOS_TEST_FOR_EXCEPTION(epvs==0, std::runtime_error,
00063     "could not cast vector space to EpetraVectorSpace in "
00064     "EpetraVector ctor");
00065 
00066   epetraVec_ = rcp(new Epetra_Vector(*(epvs->epetraMap())));
00067 }
00068 
00069 
00070 
00071 EpetraVector
00072 ::EpetraVector(const VectorSpace<double>& vs,
00073   const RCP<Epetra_Vector>& vec)
00074   : SingleChunkVector<double>(), 
00075     vecSpace_(vs), 
00076     epetraVec_(vec), 
00077     numLocalElements_(vs.numLocalElements())
00078 {
00079   const EpetraVectorSpace* epvs 
00080     = dynamic_cast<const EpetraVectorSpace*>(vs.ptr().get());
00081   TEUCHOS_TEST_FOR_EXCEPTION(epvs==0, std::runtime_error,
00082     "could not cast vector space to EpetraVectorSpace in "
00083     "EpetraVector ctor");
00084 }
00085 
00086 
00087 
00088 
00089 
00090 double& EpetraVector::operator[](int globalIndex) 
00091 {
00092   const Epetra_BlockMap& myMap = epetraVec()->Map();
00093   return (*epetraVec())[myMap.LID(globalIndex)];
00094 }
00095 
00096 void EpetraVector::setElement(int index, const double& value)
00097 {
00098   int loc_index[1] = { index };
00099   epetraVec()->ReplaceGlobalValues(1, const_cast<double*>(&value), 
00100     loc_index);
00101 }
00102 
00103 void EpetraVector::addToElement(int index, const double& value)
00104 {
00105 //  cout << "adding (" << index << ", " << value << ")" << std::endl;
00106   int loc_index[1] = { index };
00107   epetraVec()->SumIntoGlobalValues(1, const_cast<double*>(&value), 
00108     loc_index);
00109 }
00110 
00111 const double& EpetraVector::getElement(int index) const 
00112 {
00113   const Epetra_BlockMap& myMap = epetraVec()->Map();
00114   return (*epetraVec())[myMap.LID(index)];
00115 }
00116 
00117 void EpetraVector::getElements(const int* globalIndices, int numElems,
00118   Teuchos::Array<double>& elems) const
00119 {
00120   elems.resize(numElems);
00121   const Epetra_BlockMap& myMap = epetraVec()->Map();
00122   RCP<const Epetra_Vector> epv = epetraVec();
00123 
00124   for (int i=0; i<numElems; i++)
00125   {
00126     elems[i] = (*epv)[myMap.LID(globalIndices[i])];
00127   }
00128 }
00129 
00130 void EpetraVector::setElements(int numElems, const int* globalIndices,
00131   const double* values)
00132 {
00133   Epetra_FEVector* vec = dynamic_cast<Epetra_FEVector*>(epetraVec().get());
00134   int ierr = vec->ReplaceGlobalValues(numElems, globalIndices, values);
00135   TEUCHOS_TEST_FOR_EXCEPTION(ierr < 0, std::runtime_error, "ReplaceGlobalValues returned "
00136     "ierr=" << ierr << " in EpetraVector::setElements()");
00137 }
00138 
00139 void EpetraVector::addToElements(int numElems, const int* globalIndices,
00140   const double* values)
00141 {
00142   Epetra_FEVector* vec = dynamic_cast<Epetra_FEVector*>(epetraVec().get());
00143   int ierr = vec->SumIntoGlobalValues(numElems, globalIndices, values);
00144   TEUCHOS_TEST_FOR_EXCEPTION(ierr < 0, std::runtime_error, "SumIntoGlobalValues returned "
00145     "ierr=" << ierr << " in EpetraVector::addToElements()");
00146 }
00147 
00148 void EpetraVector::finalizeAssembly()
00149 {
00150   Epetra_FEVector* vec = dynamic_cast<Epetra_FEVector*>(epetraVec().get());
00151   vec->GlobalAssemble();
00152 }
00153 
00154 
00155 void EpetraVector::print(std::ostream& os) const 
00156 {
00157   epetraVec()->Print(os);
00158 }
00159 
00160 
00161 const Epetra_Vector& EpetraVector::getConcrete(const Playa::Vector<double>& tsfVec)
00162 {
00163   const EpetraVector* epv 
00164     = dynamic_cast<const EpetraVector*>(tsfVec.ptr().get());
00165   TEUCHOS_TEST_FOR_EXCEPTION(epv==0, std::runtime_error,
00166     "EpetraVector::getConcrete called on a vector that "
00167     "could not be cast to an EpetraVector");
00168   return *(epv->epetraVec());
00169 }
00170 
00171 Epetra_Vector& EpetraVector::getConcrete(Playa::Vector<double>& tsfVec)
00172 {
00173   EpetraVector* epv 
00174     = dynamic_cast<EpetraVector*>(tsfVec.ptr().get());
00175   TEUCHOS_TEST_FOR_EXCEPTION(epv==0, std::runtime_error,
00176     "EpetraVector::getConcrete called on a vector that "
00177     "could not be cast to an EpetraVector");
00178   return *(epv->epetraVec());
00179 }
00180 
00181 
00182 Epetra_Vector* EpetraVector::getConcretePtr(Playa::Vector<double>& tsfVec)
00183 {
00184   EpetraVector* epv 
00185     = dynamic_cast<EpetraVector*>(tsfVec.ptr().get());
00186   TEUCHOS_TEST_FOR_EXCEPTION(epv==0, std::runtime_error,
00187     "EpetraVector::getConcrete called on a vector that "
00188     "could not be cast to an EpetraVector");
00189   return epv->epetraVec().get();
00190 }
00191 
00192 
00193 void EpetraVector::update(const double& alpha, 
00194   const VectorBase<double>* other, const double& gamma)
00195 {
00196   const EpetraVector* epv 
00197     = dynamic_cast<const EpetraVector*>(other);
00198   epetraVec_->Update(alpha, *(epv->epetraVec_), gamma);
00199 }
00200 
00201 
00202 
00203 void EpetraVector::update(
00204   const double& alpha, const VectorBase<double>* x,
00205   const double& beta, const VectorBase<double>* y,
00206   const double& gamma)
00207 {
00208   const EpetraVector* epx 
00209     = dynamic_cast<const EpetraVector*>(x);
00210   const EpetraVector* epy
00211     = dynamic_cast<const EpetraVector*>(y);
00212 
00213   epetraVec_->Update(alpha, *(epx->epetraVec_), 
00214     beta, *(epy->epetraVec_), gamma);
00215 }
00216 
00217 
00218 double EpetraVector::dot(const VectorBase<double>* other) const 
00219 {
00220   double rtn = 0.0;
00221   const EpetraVector* epv 
00222     = dynamic_cast<const EpetraVector*>(other);
00223   epetraVec_->Dot(*(epv->epetraVec_), &rtn);
00224 
00225   return rtn;
00226 }
00227 
00228 double EpetraVector::norm2() const 
00229 {
00230   double rtn = 0.0;
00231   epetraVec_->Norm2(&rtn);
00232 
00233   return rtn;
00234 }

Site Contact