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 //
00091 void BlockedMappingStrategy::copyEpetraIntoThyra(const Epetra_MultiVector& X,
00092                                                  const Teuchos::Ptr<Thyra::MultiVectorBase<double> > & thyra_X) const
00093 {
00094    int count = X.NumVectors(); 
00095 
00096    std::vector<RCP<Epetra_MultiVector> > subX;
00097 
00098    // allocate vectors to copy into
00099    Blocking::buildSubVectors(blockMaps_,subX,count);
00100 
00101    // copy source vector to X vector
00102    Blocking::one2many(subX,X,blockImport_);
00103 
00104    // convert subX to an array of multi vectors
00105    Teuchos::Array<RCP<Thyra::MultiVectorBase<double> > > thyra_subX;
00106    Teuchos::Ptr<Thyra::ProductMultiVectorBase<double> > prod_X
00107          = Teuchos::ptr_dynamic_cast<Thyra::ProductMultiVectorBase<double> >(thyra_X);
00108    for(unsigned int i=0;i<blockMaps_.size();i++) {
00109       RCP<Thyra::DefaultSpmdMultiVector<double> > vec 
00110             = rcp_dynamic_cast<Thyra::DefaultSpmdMultiVector<double> >(prod_X->getNonconstMultiVectorBlock(i)); 
00111       fillDefaultSpmdMultiVector(vec,subX[i]);
00112    }
00113 }
00114 
00115 // Virtual function defined in MappingStrategy.  This copies
00116 // an Epetra_MultiVector into a Thyra::MultiVectorBase with
00117 // blocking handled by the strides defined in the constructor.
00118 //
00119 //   arguments:
00120 //      thyra_Y - source Thyra::MultiVectorBase
00121 //      Y       - destination Epetra_MultiVector
00122 //
00123 void BlockedMappingStrategy::copyThyraIntoEpetra(const RCP<const Thyra::MultiVectorBase<double> > & thyra_Y,
00124                                                  Epetra_MultiVector& Y) const
00125 {
00126    std::vector<RCP<const Epetra_MultiVector> > subY;
00127    RCP<const Thyra::DefaultProductMultiVector<double> > prod_Y 
00128          = rcp_dynamic_cast<const Thyra::DefaultProductMultiVector<double> >(thyra_Y);
00129 
00130    // convert thyra product vector to subY
00131    for(unsigned int i=0;i<blockMaps_.size();i++)
00132       subY.push_back(Thyra::get_Epetra_MultiVector(*blockMaps_[i].second,prod_Y->getMultiVectorBlock(i)));
00133 
00134    // endow the subVectors with required information about the maps
00135    // Blocking::associateSubVectors(blockMaps_,subY);
00136 
00137    // copy solution vectors to Y vector
00138    Blocking::many2one(Y,subY,blockExport_);
00139 }
00140 
00141 // this is the core routine that builds the maps
00142 // and importers/exporters neccessary for all the
00143 // transfers. Currently it simply calls out to the
00144 // interlaced epetra functions. (Comment: this
00145 // routine should probably be private or protected
00146 // ... it is basically the meat of the constructor)
00147 //
00148 //    arguments:
00149 //       vars - Vector describing the blocking of variables
00150 //       baseMap - basic map to use in the transfers
00151 //       comm    - Epetra_Comm object
00152 //
00153 void BlockedMappingStrategy::buildBlockTransferData(const std::vector<std::vector<int> > & vars,
00154      const Teuchos::RCP<const Epetra_Map> & baseMap, const Epetra_Comm & comm)
00155 {
00156    // build block for each vector
00157    for(std::size_t i=0;i<vars.size();i++) {
00158       // build maps and exporters/importers
00159       Blocking::MapPair mapPair = Blocking::buildSubMap(vars[i],comm);
00160       Blocking::ImExPair iePair = Blocking::buildExportImport(*baseMap, mapPair);
00161 
00162       blockMaps_.push_back(mapPair);
00163       blockImport_.push_back(iePair.first);
00164       blockExport_.push_back(iePair.second);
00165    }
00166 }
00167 
00168 // Builds a blocked Thyra operator that uses the strided
00169 // mapping strategy to define sub blocks.
00170 //
00171 //    arguments:
00172 //       mat - Epetra_CrsMatrix with FillComplete called, this
00173 //             matrix is assumed to be square, with the same
00174 //             range and domain maps
00175 //    returns: Blocked Thyra linear operator with sub blocks
00176 //             defined by this mapping strategy
00177 //
00178 const Teuchos::RCP<Thyra::BlockedLinearOpBase<double> > 
00179 BlockedMappingStrategy::buildBlockedThyraOp(const RCP<const Epetra_CrsMatrix> & crsContent,const std::string & label) const
00180 {
00181    int dim = blockMaps_.size();
00182 
00183    RCP<Thyra::DefaultBlockedLinearOp<double> > A = Thyra::defaultBlockedLinearOp<double>();
00184 
00185    A->beginBlockFill(dim,dim);
00186    for(int i=0;i<dim;i++) {
00187       for(int j=0;j<dim;j++) {
00188          // label block correctly
00189          std::stringstream ss;
00190          ss << label << "_" << i << "," << j;
00191 
00192          // build the blocks and place it the right location
00193          RCP<Epetra_CrsMatrix> blk = Blocking::buildSubBlock(i,j,*crsContent,blockMaps_);
00194          A->setNonconstBlock(i,j,Thyra::nonconstEpetraLinearOp(blk,ss.str()));
00195       }
00196    } // end for i
00197    A->endBlockFill();
00198 
00199    return A;
00200 }
00201 
00202 // Rebuilds a blocked Thyra operator that uses the strided
00203 // mapping strategy to define sub blocks.
00204 //
00205 //    arguments:
00206 //       crsContent - Epetra_CrsMatrix with FillComplete called, this
00207 //                    matrix is assumed to be square, with the same
00208 //                    range and domain maps
00209 //       A - Destination block linear op composed of blocks of
00210 //           Epetra_CrsMatrix at all relevant locations
00211 //
00212 void BlockedMappingStrategy::rebuildBlockedThyraOp(const RCP<const Epetra_CrsMatrix> & crsContent,
00213                                                    const RCP<Thyra::BlockedLinearOpBase<double> > & A) const
00214 {
00215    int dim = blockMaps_.size();
00216 
00217    for(int i=0;i<dim;i++) {
00218       for(int j=0;j<dim;j++) {
00219          // get Epetra version of desired block
00220          RCP<Thyra::LinearOpBase<double> > Aij = A->getNonconstBlock(i,j);
00221          RCP<Epetra_CrsMatrix> eAij = rcp_dynamic_cast<Epetra_CrsMatrix>(Thyra::get_Epetra_Operator(*Aij),true);
00222 
00223          // rebuild the blocks and place it the right location
00224          Blocking::rebuildSubBlock(i,j,*crsContent,blockMaps_,*eAij);
00225       }
00226    } // end for i
00227 }
00228 
00229 } // end namespace Epetra
00230 } // end namespace Teko
 All Classes Files Functions Variables