Teko Version of the Day
Teko_PreconditionerFactory.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_Config.h"
00048 #include "Teko_PreconditionerFactory.hpp"
00049 
00050 #include "Teko_InverseLibrary.hpp"
00051 #include "Teko_Preconditioner.hpp"
00052 
00053 // Specific preconditioners included for dynamic creation
00054 #include "Teko_JacobiPreconditionerFactory.hpp"
00055 #include "Teko_GaussSeidelPreconditionerFactory.hpp"
00056 #include "Teko_AddPreconditionerFactory.hpp"
00057 #include "Teko_MultPreconditionerFactory.hpp"
00058 #include "Teko_LU2x2PreconditionerFactory.hpp"
00059 #include "Teko_IterativePreconditionerFactory.hpp"
00060 #include "Teko_DiagnosticPreconditionerFactory.hpp"
00061 #include "Teko_DiagonallyScaledPreconditionerFactory.hpp"
00062 #include "Teko_DiagonalPreconditionerFactory.hpp"
00063 #include "Teko_ProbingPreconditionerFactory.hpp"
00064 #include "Teko_IdentityPreconditionerFactory.hpp"
00065 #include "NS/Teko_LSCPreconditionerFactory.hpp"
00066 #include "NS/Teko_SIMPLEPreconditionerFactory.hpp"
00067 
00068 #ifdef Teko_ENABLE_ML_SMOOTHERS
00069 #include "Teko_SmootherPreconditionerFactory.hpp"
00070 #include "Teko_MLPreconditionerFactory.hpp"
00071 #endif
00072 
00073 
00074 #include "Thyra_DefaultPreconditioner.hpp"
00075 
00076 using namespace Thyra;
00077 using Teuchos::RCP;
00078 
00079 namespace Teko {
00081 
00083 bool PreconditionerFactory::isCompatible(const Thyra::LinearOpSourceBase<double> &fwdOpSrc) const
00084 {
00085    RCP<const Thyra::LinearOpBase<double> > A = fwdOpSrc.getOp();
00086    return A!=Teuchos::null;
00087 }
00088 
00090 RCP<Thyra::PreconditionerBase<double> > PreconditionerFactory::createPrec() const
00091 {
00092    // build a preconditioner, give it some inital state
00093    RCP<Preconditioner> bp = rcp(new Preconditioner());
00094    bp->setStateObject(buildPreconditionerState());
00095    bp->getStateObject()->setInitialized(false);
00096 
00097    return bp;
00098 }
00099 
00101 void PreconditionerFactory::initializePrec(const RCP<const LinearOpSourceBase<double> > & ASrc,
00102                     PreconditionerBase<double> * prec,
00103                     const ESupportSolveUse supportSolveUse) const
00104 {
00105    // get the blocked linear operator
00106    LinearOp A = Teuchos::rcp_const_cast<Thyra::LinearOpBase<double> >(ASrc->getOp());
00107 
00108    Preconditioner * blkPrec = dynamic_cast<Preconditioner *>(prec);
00109    TEUCHOS_ASSERT(blkPrec!=0);
00110  
00111    // grab the state object
00112    RCP<PreconditionerState> state = blkPrec->getStateObject();
00113    state->setInitialized(false);
00114 
00115    // build the preconditioner
00116    const RCP<const LinearOpBase<double> > M = buildPreconditionerOperator(A,*state);
00117 
00118    // set the request handler for the 
00119    setOpRequestHandler(*this,M);
00120 
00121    // must first cast that to be initialized
00122    DefaultPreconditioner<double> & dPrec = Teuchos::dyn_cast<DefaultPreconditioner<double> >(*prec);
00123    dPrec.initializeUnspecified(Teuchos::rcp_const_cast<LinearOpBase<double> >(M));
00124 }
00125 
00127 void PreconditionerFactory::initializePrec(const RCP<const LinearOpSourceBase<double> > & ASrc,
00128                                                 const RCP<const Thyra::MultiVectorBase<double> > & solnVec,
00129                                                 PreconditionerBase<double> * prec,
00130                                                 const ESupportSolveUse supportSolveUse) const
00131 {
00132    Preconditioner * blkPrec = dynamic_cast<Preconditioner *>(prec);
00133    blkPrec->setSourceVector(Teuchos::rcp_const_cast<Thyra::MultiVectorBase<double> >(solnVec));
00134 
00135    initializePrec(ASrc,prec,supportSolveUse);
00136 }
00137 
00139 void PreconditionerFactory::uninitializePrec(PreconditionerBase<double> * prec, 
00140                       RCP<const LinearOpSourceBase<double> > * fwdOpSrc,
00141                       ESupportSolveUse *supportSolveUse) const
00142 {
00143    // Preconditioner * blkPrec = dynamic_cast<Preconditioner *>(prec);
00144 
00145    // what do I do here?
00146    TEUCHOS_TEST_FOR_EXCEPT_MSG(true,"\"PreconditionerFactory::uninitializePrec not implemented\"");
00147 }
00148 
00149 // for ParameterListAcceptor
00151 
00153 void PreconditionerFactory::setParameterList(const RCP<Teuchos::ParameterList> & paramList)
00154 {
00155    paramList_ = paramList;
00156 }
00157 
00159 RCP< Teuchos::ParameterList > PreconditionerFactory::getNonconstParameterList()
00160 {
00161    return paramList_;
00162 }
00163 
00165 RCP< Teuchos::ParameterList > PreconditionerFactory::unsetParameterList()
00166 {
00167    RCP<Teuchos::ParameterList> _paramList = paramList_;
00168    paramList_ = Teuchos::null;
00169    return _paramList;
00170 }
00171 
00173 void PreconditionerFactory::setInverseLibrary(const RCP<const InverseLibrary> & il)
00174 {
00175    inverseLibrary_ = il;
00176 }
00177 
00179 RCP<const InverseLibrary> PreconditionerFactory::getInverseLibrary() const
00180 {
00181    // lazily build the inverse library only when needed
00182    if(inverseLibrary_==Teuchos::null)
00183       return InverseLibrary::buildFromStratimikos();
00184 
00185    return inverseLibrary_;
00186 }
00187 
00189 // Static members and methods
00191 
00193 void PreconditionerFactory::setOpRequestHandler(const RequestHandlerContainer & rhc,const LinearOp & op)
00194 {
00195    ModifiableLinearOp mlo = Teuchos::rcp_const_cast<Thyra::LinearOpBase<double> >(op);
00196 
00197    // conditionally set the request handler
00198    RCP<RequestHandlerContainer> reqHandCont = Teuchos::rcp_dynamic_cast<RequestHandlerContainer>(mlo);
00199    if(reqHandCont!=Teuchos::null) {
00200       reqHandCont->setRequestHandler(rhc.getRequestHandler());
00201    }
00202    else {
00203       // is null
00204    }
00205    
00206 }
00207 
00209 CloneFactory<PreconditionerFactory> PreconditionerFactory::precFactoryBuilder_;
00210 
00225 RCP<PreconditionerFactory> 
00226 PreconditionerFactory::buildPreconditionerFactory(const std::string & name,
00227                                                        const Teuchos::ParameterList & settings,
00228                                                        const RCP<const InverseLibrary> & invLib)
00229 {
00230    Teko_DEBUG_SCOPE("PreconditionerFactory::buildPreconditionerFactory",10);
00231 
00232    // initialize the defaults if necessary
00233    if(precFactoryBuilder_.cloneCount()==0) initializePrecFactoryBuilder();
00234 
00235    // request the preconditioner factory from the CloneFactory
00236    RCP<PreconditionerFactory> precFact = precFactoryBuilder_.build(name);
00237 
00238    Teko_DEBUG_MSG_BEGIN(5);
00239       DEBUG_STREAM << "Looked up \"" << name << "\"" << std::endl;
00240       DEBUG_STREAM << "Built " << precFact << std::endl;
00241    Teko_DEBUG_MSG_END();
00242 
00243    if(precFact==Teuchos::null)  
00244       return Teuchos::null;
00245 
00246    // add in the inverse library
00247    if(invLib!=Teuchos::null) {
00248       precFact->setInverseLibrary(invLib);
00249       precFact->setRequestHandler(invLib->getRequestHandler());
00250    }
00251 
00252    // now that inverse library has been set,
00253    // pass in the parameter list
00254    precFact->initializeFromParameterList(settings);
00255 
00256    return precFact;
00257 }
00258 
00272 void PreconditionerFactory::addPreconditionerFactory(const std::string & name,const RCP<Cloneable> & clone)
00273 {
00274    // initialize the defaults if necessary
00275    if(precFactoryBuilder_.cloneCount()==0) initializePrecFactoryBuilder();
00276 
00277    // add clone to builder
00278    precFactoryBuilder_.addClone(name,clone); 
00279 }
00280 
00282 void PreconditionerFactory::initializePrecFactoryBuilder()
00283 {
00284    RCP<Cloneable> clone;
00285 
00286    // add various preconditioners to factory
00287    clone = rcp(new AutoClone<LU2x2PreconditionerFactory>());
00288    precFactoryBuilder_.addClone("Block LU2x2",clone);
00289 
00290    clone = rcp(new AutoClone<JacobiPreconditionerFactory>());
00291    precFactoryBuilder_.addClone("Block Jacobi",clone);
00292 
00293    clone = rcp(new AutoClone<GaussSeidelPreconditionerFactory>());
00294    precFactoryBuilder_.addClone("Block Gauss-Seidel",clone);
00295 
00296    clone = rcp(new AutoClone<AddPreconditionerFactory>());
00297    precFactoryBuilder_.addClone("Block Add",clone);
00298 
00299    clone = rcp(new AutoClone<MultPreconditionerFactory>());
00300    precFactoryBuilder_.addClone("Block Multiply",clone);
00301 
00302    clone = rcp(new AutoClone<NS::LSCPreconditionerFactory>());
00303    precFactoryBuilder_.addClone("NS LSC",clone);
00304 
00305    clone = rcp(new AutoClone<NS::SIMPLEPreconditionerFactory>());
00306    precFactoryBuilder_.addClone("NS SIMPLE",clone);
00307 
00308    clone = rcp(new AutoClone<IterativePreconditionerFactory>());
00309    precFactoryBuilder_.addClone("Iterative Preconditioner",clone);
00310 
00311    clone = rcp(new AutoClone<DiagonalPreconditionerFactory>());
00312    precFactoryBuilder_.addClone("Explicit Diagonal Preconditioner",clone);
00313 
00314    clone = rcp(new AutoClone<DiagnosticPreconditionerFactory>());
00315    precFactoryBuilder_.addClone("Diagnostic Inverse",clone);
00316 
00317    clone = rcp(new AutoClone<DiagonallyScaledPreconditionerFactory>());
00318    precFactoryBuilder_.addClone("Diagonal Scaling",clone);
00319 
00320    clone = rcp(new AutoClone<IdentityPreconditionerFactory>());
00321    precFactoryBuilder_.addClone("Identity",clone);
00322 
00323 #ifdef Teko_ENABLE_Isorropia
00324    clone = rcp(new AutoClone<ProbingPreconditionerFactory>());
00325    precFactoryBuilder_.addClone("Probing Preconditioner",clone);
00326 #endif
00327 
00328 #ifdef Teko_ENABLE_ML_SMOOTHERS
00329    clone = rcp(new AutoClone<MLPreconditionerFactory>());
00330    precFactoryBuilder_.addClone("Blocked ML Preconditioner",clone);
00331 #endif
00332 }
00333 
00334 void PreconditionerFactory::getPreconditionerFactoryNames(std::vector<std::string> & names)
00335 { 
00336    // initialize the defaults if necessary
00337    if(precFactoryBuilder_.cloneCount()==0) initializePrecFactoryBuilder();
00338    precFactoryBuilder_.getCloneNames(names); 
00339 }
00340 
00341 } // end namespace Teko
 All Classes Files Functions Variables