Teko Version of the Day
Teko_StridedMappingStrategy.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 "Epetra/Teko_StridedMappingStrategy.hpp"
00048 #include "Epetra/Teko_InterlacedEpetra.hpp"
00049 #include "Epetra/Teko_EpetraHelpers.hpp"
00050 
00051 #include "Thyra_EpetraThyraWrappers.hpp"
00052 #include "Thyra_EpetraLinearOp.hpp"
00053 #include "Thyra_DefaultProductMultiVector.hpp"
00054 #include "Thyra_DefaultProductVectorSpace.hpp"
00055 #include "Thyra_DefaultSpmdMultiVector.hpp"
00056 #include "Thyra_DefaultBlockedLinearOp.hpp"
00057 #include "Thyra_get_Epetra_Operator.hpp"
00058 
00059 using Teuchos::RCP;
00060 using Teuchos::rcp;
00061 using Teuchos::rcp_dynamic_cast;
00062 
00063 namespace Teko {
00064 namespace Epetra {
00065 
00066 // Creates a strided mapping strategy. This class is useful
00067 // for breaking up nodally ordered matrices (i.e. the unknowns
00068 // in a FEM problem are ordered [u0,v0,p0,u1,v1,p1,...]). Current
00069 // implimentation only supports a fixed number of variables
00070 //
00071 //    arguments: 
00072 //       vars - Number of different variables 
00073 //       map  - original Epetra_Map to be broken up
00074 //       comm - Epetra_Comm object related to the map
00075 //
00076 StridedMappingStrategy::StridedMappingStrategy(const std::vector<int> & vars,const RCP<const Epetra_Map> & map,
00077                                                const Epetra_Comm & comm)
00078 {
00079    rangeMap_ = map;
00080    domainMap_ = map;
00081    buildBlockTransferData(vars, rangeMap_,comm);
00082 }
00083 
00084 // Virtual function defined in MappingStrategy.  This copies
00085 // an Epetra_MultiVector into a Thyra::MultiVectorBase with
00086 // blocking handled by the strides defined in the constructor.
00087 //
00088 //   arguments:
00089 //      X       - source Epetra_MultiVector
00090 //      thyra_X - destination Thyra::MultiVectorBase
00091 //
00092 void StridedMappingStrategy::copyEpetraIntoThyra(const Epetra_MultiVector& X,
00093                                                  const Teuchos::Ptr<Thyra::MultiVectorBase<double> > & thyra_X) const 
00094 {
00095    int count = X.NumVectors(); 
00096 
00097    std::vector<RCP<Epetra_MultiVector> > subX;
00098 
00099    // allocate vectors to copy into
00100    Strided::buildSubVectors(blockMaps_,subX,count);
00101 
00102    // copy source vector to X vector
00103    Strided::one2many(subX,X,blockImport_);
00104 
00105    // convert subX to an array of multi vectors
00106    Teuchos::Array<RCP<Thyra::MultiVectorBase<double> > > thyra_subX;
00107    Teuchos::Ptr<Thyra::ProductMultiVectorBase<double> > prod_X
00108          = Teuchos::ptr_dynamic_cast<Thyra::ProductMultiVectorBase<double> >(thyra_X);
00109    for(unsigned int i=0;i<blockMaps_.size();i++) {
00110       RCP<Thyra::DefaultSpmdMultiVector<double> > vec 
00111             = rcp_dynamic_cast<Thyra::DefaultSpmdMultiVector<double> >(prod_X->getNonconstMultiVectorBlock(i)); 
00112       fillDefaultSpmdMultiVector(vec,subX[i]);
00113    }
00114 }
00115 
00116 // Virtual function defined in MappingStrategy.  This copies
00117 // an Epetra_MultiVector into a Thyra::MultiVectorBase with
00118 // blocking handled by the strides defined in the constructor.
00119 //
00120 //   arguments:
00121 //      thyra_Y - source Thyra::MultiVectorBase
00122 //      Y       - destination Epetra_MultiVector
00123 //
00124 void StridedMappingStrategy::copyThyraIntoEpetra(const RCP<const Thyra::MultiVectorBase<double> > & thyra_Y,
00125                                                  Epetra_MultiVector& Y) const
00126 {
00127    std::vector<RCP<const Epetra_MultiVector> > subY;
00128    RCP<const Thyra::DefaultProductMultiVector<double> > prod_Y 
00129          = rcp_dynamic_cast<const Thyra::DefaultProductMultiVector<double> >(thyra_Y);
00130 
00131    // convert thyra product vector to subY
00132    for(unsigned int i=0;i<blockMaps_.size();i++)
00133       subY.push_back(Thyra::get_Epetra_MultiVector(*blockMaps_[i].second,prod_Y->getMultiVectorBlock(i)));
00134 
00135    // endow the subVectors with required information about the maps
00136    Strided::associateSubVectors(blockMaps_,subY);
00137 
00138    // copy solution vectors to Y vector
00139    Strided::many2one(Y,subY,blockExport_);
00140 }
00141 
00142 // this is the core routine that builds the maps
00143 // and importers/exporters neccessary for all the
00144 // transfers. Currently it simply calls out to the
00145 // interlaced epetra functions. (Comment: this
00146 // routine should probably be private or protected
00147 // ... it is basically the meat of the constructor)
00148 //
00149 //    arguments:
00150 //       vars - Vector describing the blocking of variables
00151 //       baseMap - basic map to use in the transfers
00152 //       comm    - Epetra_Comm object
00153 //
00154 void StridedMappingStrategy::buildBlockTransferData(const std::vector<int> & vars,const Teuchos::RCP<const Epetra_Map> & baseMap, const Epetra_Comm & comm)
00155 {
00156    // build maps and exporters/importers
00157    Strided::buildSubMaps(*baseMap,vars,comm,blockMaps_);
00158    Strided::buildExportImport(*baseMap, blockMaps_, blockExport_,blockImport_);
00159 }
00160 
00161 // Builds a blocked Thyra operator that uses the strided
00162 // mapping strategy to define sub blocks.
00163 //
00164 //    arguments:
00165 //       mat - Epetra_CrsMatrix with FillComplete called, this
00166 //             matrix is assumed to be square, with the same
00167 //             range and domain maps
00168 //    returns: Blocked Thyra linear operator with sub blocks
00169 //             defined by this mapping strategy
00170 //
00171 const Teuchos::RCP<Thyra::BlockedLinearOpBase<double> > 
00172 StridedMappingStrategy::buildBlockedThyraOp(const RCP<const Epetra_CrsMatrix> & crsContent,const std::string & label) const
00173 {
00174    int dim = blockMaps_.size();
00175 
00176    RCP<Thyra::DefaultBlockedLinearOp<double> > A = Thyra::defaultBlockedLinearOp<double>();
00177 
00178    A->beginBlockFill(dim,dim);
00179    for(int i=0;i<dim;i++) {
00180       for(int j=0;j<dim;j++) {
00181          // label block correctly
00182          std::stringstream ss;
00183          ss << label << "_" << i << "," << j;
00184 
00185          // build the blocks and place it the right location
00186          A->setNonconstBlock(i,j,Thyra::nonconstEpetraLinearOp(Strided::buildSubBlock(i,j,*crsContent,blockMaps_),ss.str()));
00187       }
00188    } // end for i
00189    A->endBlockFill();
00190 
00191    return A;
00192 }
00193 
00194 // Rebuilds a blocked Thyra operator that uses the strided
00195 // mapping strategy to define sub blocks.
00196 //
00197 //    arguments:
00198 //       crsContent - Epetra_CrsMatrix with FillComplete called, this
00199 //                    matrix is assumed to be square, with the same
00200 //                    range and domain maps
00201 //       A - Destination block linear op composed of blocks of
00202 //           Epetra_CrsMatrix at all relevant locations
00203 //
00204 void StridedMappingStrategy::rebuildBlockedThyraOp(const RCP<const Epetra_CrsMatrix> & crsContent,
00205                                                    const RCP<Thyra::BlockedLinearOpBase<double> > & A) const
00206 {
00207    int dim = blockMaps_.size();
00208 
00209    for(int i=0;i<dim;i++) {
00210       for(int j=0;j<dim;j++) {
00211          // get Epetra version of desired block
00212          RCP<Thyra::LinearOpBase<double> > Aij = A->getNonconstBlock(i,j);
00213          RCP<Epetra_CrsMatrix> eAij = rcp_dynamic_cast<Epetra_CrsMatrix>(Thyra::get_Epetra_Operator(*Aij),true);
00214 
00215          // rebuild the blocks and place it the right location
00216          Strided::rebuildSubBlock(i,j,*crsContent,blockMaps_,*eAij);
00217       }
00218    } // end for i
00219 }
00220 
00221 } // end namespace Epetra
00222 } // end namespace Teko
 All Classes Files Functions Variables