Teko Version of the Day
Teko_EpetraHelpers.cpp
00001 /*
00002 // @HEADER
00003 // 
00004 // ***********************************************************************
00005 // 
00006 //      Teko: A package for block and physics based preconditioning
00007 //                  Copyright 2010 Sandia Corporation 
00008 //  
00009 // Under the terms of Contract DE-AC04-94AL85000 with Sandia Corporation,
00010 // the U.S. Government retains certain rights in this software.
00011 //  
00012 // Redistribution and use in source and binary forms, with or without
00013 // modification, are permitted provided that the following conditions are
00014 // met:
00015 //  
00016 // 1. Redistributions of source code must retain the above copyright
00017 // notice, this list of conditions and the following disclaimer.
00018 //  
00019 // 2. Redistributions in binary form must reproduce the above copyright
00020 // notice, this list of conditions and the following disclaimer in the
00021 // documentation and/or other materials provided with the distribution.
00022 //  
00023 // 3. Neither the name of the Corporation nor the names of the
00024 // contributors may be used to endorse or promote products derived from
00025 // this software without specific prior written permission. 
00026 //  
00027 // THIS SOFTWARE IS PROVIDED BY SANDIA CORPORATION "AS IS" AND ANY
00028 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00029 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
00030 // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SANDIA CORPORATION OR THE
00031 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
00032 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
00033 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
00034 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
00035 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 
00036 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
00037 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00038 //  
00039 // Questions? Contact Eric C. Cyr (eccyr@sandia.gov)
00040 // 
00041 // ***********************************************************************
00042 // 
00043 // @HEADER
00044 
00045 */
00046 
00047 #include "Teko_EpetraHelpers.hpp"
00048 
00049 // Thyra Includes
00050 #include "Thyra_EpetraLinearOp.hpp"
00051 #include "Thyra_BlockedLinearOpBase.hpp"
00052 #include "Thyra_DefaultMultipliedLinearOp.hpp"
00053 #include "Thyra_DefaultDiagonalLinearOp.hpp"
00054 #include "Thyra_DefaultZeroLinearOp.hpp"
00055 #include "Thyra_DefaultBlockedLinearOp.hpp"
00056 #include "Thyra_EpetraThyraWrappers.hpp"
00057 #include "Thyra_SpmdVectorBase.hpp"
00058 #include "Thyra_SpmdVectorSpaceBase.hpp"
00059 
00060 // Epetra includes
00061 #include "Epetra_Vector.h"
00062 
00063 // EpetraExt includes
00064 #include "EpetraExt_ProductOperator.h"
00065 #include "EpetraExt_MatrixMatrix.h"
00066 
00067 // Teko includes
00068 #include "Teko_EpetraOperatorWrapper.hpp"
00069 #include "Teko_Utilities.hpp"
00070 
00071 using Teuchos::RCP;
00072 using Teuchos::rcp;
00073 using Teuchos::rcpFromRef;
00074 using Teuchos::rcp_dynamic_cast;
00075 using Teuchos::null;
00076 
00077 namespace Teko {
00078 namespace Epetra {
00079 
00090 const Teuchos::RCP<const Thyra::LinearOpBase<double> > thyraDiagOp(const RCP<const Epetra_Vector> & ev,const Epetra_Map & map,
00091                                                                    const std::string & lbl)
00092 {
00093    const RCP<const Thyra::VectorBase<double> > thyraVec  // need a Thyra::VectorBase object
00094          = Thyra::create_Vector(ev,Thyra::create_VectorSpace(rcpFromRef(map)));
00095    Teuchos::RCP<Thyra::LinearOpBase<double> > op 
00096          = Teuchos::rcp(new Thyra::DefaultDiagonalLinearOp<double>(thyraVec));
00097    op->setObjectLabel(lbl);
00098    return op;
00099 }
00100 
00111 const Teuchos::RCP<Thyra::LinearOpBase<double> > thyraDiagOp(const RCP<Epetra_Vector> & ev,const Epetra_Map & map,
00112                                                                    const std::string & lbl)
00113 {
00114    const RCP<Thyra::VectorBase<double> > thyraVec  // need a Thyra::VectorBase object
00115          = Thyra::create_Vector(ev,Thyra::create_VectorSpace(rcpFromRef(map)));
00116    Teuchos::RCP<Thyra::LinearOpBase<double> > op 
00117          = Teuchos::rcp(new Thyra::DefaultDiagonalLinearOp<double>(thyraVec));
00118    op->setObjectLabel(lbl);
00119    return op;
00120 }
00121 
00131 void fillDefaultSpmdMultiVector(Teuchos::RCP<Thyra::DefaultSpmdMultiVector<double> > & spmdMV,
00132                                 Teuchos::RCP<Epetra_MultiVector> & epetraMV)
00133 {
00134    // first get desired range and domain
00135    const RCP<const Thyra::SpmdVectorSpaceBase<double> > range  = spmdMV->spmdSpace();
00136    const RCP<const Thyra::ScalarProdVectorSpaceBase<double> > domain 
00137          = rcp_dynamic_cast<const Thyra::ScalarProdVectorSpaceBase<double> >(spmdMV->domain());
00138 
00139    TEUCHOS_ASSERT(domain->dim()==epetraMV->NumVectors());
00140 
00141    // New local view of raw data
00142    double *localValues; int leadingDim;
00143    if(epetraMV->ConstantStride() )
00144       epetraMV->ExtractView( &localValues, &leadingDim );
00145    else
00146       TEST_FOR_EXCEPT(true); // ToDo: Implement views of non-contiguous mult-vectors!
00147 
00148    // Build the MultiVector
00149    spmdMV->initialize(range, domain,
00150                      Teuchos::arcp(localValues,0,leadingDim*epetraMV->NumVectors(),false),
00151                      leadingDim);
00152 
00153    // make sure the Epetra_MultiVector doesn't disappear prematurely
00154    Teuchos::set_extra_data<RCP<Epetra_MultiVector> >(epetraMV,"Epetra_MultiVector",Teuchos::outArg(spmdMV));
00155 }
00156 
00166 void identityRowIndices(const Epetra_Map & rowMap, const Epetra_CrsMatrix & mat,std::vector<int> & outIndices)
00167 {
00168    int maxSz = mat.GlobalMaxNumEntries();
00169    std::vector<double> values(maxSz);
00170    std::vector<int> indices(maxSz);
00171 
00172    // loop over elements owned by this processor
00173    for(int i=0;i<rowMap.NumMyElements();i++) {
00174       bool rowIsIdentity = true;
00175       int sz = 0;
00176       int rowGID = rowMap.GID(i);
00177       mat.ExtractGlobalRowCopy(rowGID,maxSz,sz,&values[0],&indices[0]);
00178 
00179       // loop over the columns of this row
00180       for(int j=0;j<sz;j++) {
00181          int colGID = indices[j];
00182 
00183          // look at row entries
00184          if(colGID==rowGID) rowIsIdentity &= values[j]==1.0;
00185          else               rowIsIdentity &= values[j]==0.0;
00186 
00187          // not a dirchlet row...quit
00188          if(not rowIsIdentity)
00189             break;
00190       }
00191 
00192       // save a row that is dirchlet
00193       if(rowIsIdentity)
00194          outIndices.push_back(rowGID);
00195    }
00196 }
00197 
00208 void zeroMultiVectorRowIndices(Epetra_MultiVector & mv,const std::vector<int> & zeroIndices)
00209 {
00210    int colCnt = mv.NumVectors();
00211    std::vector<int>::const_iterator itr;
00212  
00213    // loop over the indices to zero
00214    for(itr=zeroIndices.begin();itr!=zeroIndices.end();++itr) {
00215  
00216       // loop over columns
00217       for(int j=0;j<colCnt;j++)
00218          TEST_FOR_EXCEPT(mv.ReplaceGlobalValue(*itr,j,0.0));
00219    }
00220 }
00221 
00231 ZeroedOperator::ZeroedOperator(const std::vector<int> & zeroIndices,
00232                                const Teuchos::RCP<const Epetra_Operator> & op)
00233    : zeroIndices_(zeroIndices), epetraOp_(op)
00234 { }
00235 
00237 int ZeroedOperator::Apply(const Epetra_MultiVector& X, Epetra_MultiVector& Y) const
00238 {
00239 /*
00240    Epetra_MultiVector temp(X);
00241    zeroMultiVectorRowIndices(temp,zeroIndices_);
00242    int result = epetraOp_->Apply(temp,Y);
00243 */
00244 
00245    int result = epetraOp_->Apply(X,Y);
00246 
00247    // zero a few of the rows
00248    zeroMultiVectorRowIndices(Y,zeroIndices_);
00249 
00250    return result;
00251 }
00252 
00253 } // end namespace Epetra
00254 } // end namespace Teko
 All Classes Files Functions Variables