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