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