Teko Version of the Day
Teko_PreconditionerInverseFactory.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 /*
00048 // @header
00049 // 
00050 // ***********************************************************************
00051 // 
00052 //      teko: a package for block and physics based preconditioning
00053 //                  copyright 2010 sandia corporation 
00054 //  
00055 // under the terms of contract de-ac04-94al85000 with sandia corporation,
00056 // the u.s. government retains certain rights in this software. 
00057 //  
00058 // redistribution and use in source and binary forms, with or without
00059 // modification, are permitted provided that the following conditions are
00060 // met:
00061 //  
00062 // 1. redistributions of source code must retain the above copyright
00063 // notice, this list of conditions and the following disclaimer.
00064 //  
00065 // 2. redistributions in binary form must reproduce the above copyright
00066 // notice, this list of conditions and the following disclaimer in the
00067 // documentation and/or other materials provided with the distribution.
00068 //  
00069 // 3. neither the name of the corporation nor the names of the
00070 // contributors may be used to endorse or promote products derived from
00071 // this software without specific prior written permission. 
00072 //  
00073 // this software is provided by sandia corporation "as is" and any
00074 // express or implied warranties, including, but not limited to, the
00075 // implied warranties of merchantability and fitness for a particular
00076 // purpose are disclaimed. in no event shall sandia corporation or the
00077 // contributors be liable for any direct, indirect, incidental, special,
00078 // exemplary, or consequential damages (including, but not limited to,
00079 // procurement of substitute goods or services; loss of use, data, or
00080 // profits; or business interruption) however caused and on any theory of
00081 // liability, whether in contract, strict liability, or tort (including 
00082 // negligence or otherwise) arising in any way out of the use of this
00083 // software, even if advised of the possibility of such damage.
00084 //  
00085 // questions? contact eric c. cyr (eccyr@sandia.gov)
00086 // 
00087 // ***********************************************************************
00088 // 
00089 // @header
00090 */
00091 
00092 #include "Teko_PreconditionerInverseFactory.hpp"
00093 
00094 // Thyra includes
00095 #include "Thyra_DefaultLinearOpSource.hpp"
00096 #include "Thyra_DefaultInverseLinearOp.hpp"
00097 #include "Thyra_DefaultPreconditioner.hpp"
00098 
00099 // Stratimikos includes
00100 #include "Stratimikos_DefaultLinearSolverBuilder.hpp"
00101 
00102 // Teko includes
00103 #include "Teko_Utilities.hpp"
00104 #include "Teko_BlockPreconditionerFactory.hpp"
00105 #include "Teko_Preconditioner.hpp"
00106 #include "Teko_PreconditionerLinearOp.hpp"
00107 #include "Teko_SolveInverseFactory.hpp"
00108 
00109 using Teuchos::rcp;
00110 using Teuchos::rcp_const_cast;
00111 using Teuchos::rcp_dynamic_cast;
00112 using Teuchos::RCP;
00113 
00114 namespace Teko {
00115 
00125 PreconditionerInverseFactory::PreconditionerInverseFactory(
00126            const Teuchos::RCP<Thyra::PreconditionerFactoryBase<double> > & precFactory,
00127            const Teuchos::RCP<Teko::RequestHandler> & rh)
00128    : precFactory_(precFactory)
00129 { 
00130    setRequestHandler(rh);
00131 }
00132 
00147 PreconditionerInverseFactory::PreconditionerInverseFactory(
00148               const Teuchos::RCP<Thyra::PreconditionerFactoryBase<double> > & precFactory,
00149               const Teuchos::RCP<const Teuchos::ParameterList> & xtraParam,
00150               const Teuchos::RCP<Teko::RequestHandler> & rh)
00151    : precFactory_(precFactory)
00152 { 
00153    if(xtraParam!=Teuchos::null)
00154       extraParams_ = rcp(new Teuchos::ParameterList(*xtraParam));
00155    else
00156       extraParams_ = Teuchos::null; // make it explicit
00157 
00158    setRequestHandler(rh);
00159 }
00160 
00162 PreconditionerInverseFactory::PreconditionerInverseFactory(const PreconditionerInverseFactory & pFactory)
00163    : precFactory_(pFactory.precFactory_)
00164 { 
00165    setRequestHandler(pFactory.getRequestHandler());
00166 }
00167 
00177 InverseLinearOp PreconditionerInverseFactory::buildInverse(const LinearOp & linearOp) const
00178 {
00179    RCP<Thyra::PreconditionerBase<double> > prec = precFactory_->createPrec();
00180    precFactory_->initializePrec(Thyra::defaultLinearOpSource(linearOp),&*prec);
00181 
00182    RCP<Teko::PreconditionerLinearOp<double> > precOp 
00183          = rcp(new Teko::PreconditionerLinearOp<double>(prec));
00184 
00185    return precOp;
00186 }
00187 
00200 InverseLinearOp PreconditionerInverseFactory::buildInverse(const LinearOp & linearOp, const PreconditionerState & parentState) const
00201 { 
00202    Teko_DEBUG_SCOPE("PreconditionerInverseFactory::buildInverse(A,parentState)",10);
00203    RCP<Thyra::PreconditionerBase<double> > prec = precFactory_->createPrec();
00204 
00205    {
00206       Teko_DEBUG_SCOPE("Casting to Teko::Preconditioner",10);
00207       // pass state downward if a Teko::Preconditioner object is begin used
00208       RCP<Teko::Preconditioner> tekoPrec = Teuchos::rcp_dynamic_cast<Teko::Preconditioner>(prec);
00209       if(tekoPrec!=Teuchos::null) {
00210          Teko_DEBUG_SCOPE("Merging states",10);
00211          tekoPrec->mergeStateObject(parentState);
00212       }
00213    }
00214 
00215    precFactory_->initializePrec(Thyra::defaultLinearOpSource(linearOp),&*prec);
00216 
00217    RCP<Teko::PreconditionerLinearOp<double> > precOp 
00218          = rcp(new Teko::PreconditionerLinearOp<double>(prec));
00219 
00220    return precOp;
00221 }
00222 
00234 void PreconditionerInverseFactory::rebuildInverse(const LinearOp & source,InverseLinearOp & dest) const
00235 {
00236    Teko_DEBUG_MSG("BEGIN PreconditionerInverseFactory::rebuildInverse",10);
00237 
00238    RCP<Thyra::PreconditionerBase<double> > prec 
00239          = Teuchos::rcp_dynamic_cast<Teko::PreconditionerLinearOp<double> >(dest)->getNonconstPreconditioner();
00240 
00241    precFactory_->initializePrec(Thyra::defaultLinearOpSource(source),&*prec);
00242 
00243    Teko_DEBUG_MSG("END PreconditionerInverseFactory::rebuildInverse",10);
00244 }
00245 
00254 Teuchos::RCP<const Teuchos::ParameterList> PreconditionerInverseFactory::getParameterList() const
00255 { 
00256    return precFactory_->getParameterList(); 
00257 }
00258 
00273 Teuchos::RCP<Teuchos::ParameterList> PreconditionerInverseFactory::getRequestedParameters() const
00274 {
00275    Teuchos::RCP<BlockPreconditionerFactory> bpf = rcp_dynamic_cast<BlockPreconditionerFactory>(precFactory_);
00276 
00277    // request the parameters from a BPF is required
00278    if(bpf!=Teuchos::null) 
00279       return bpf->getRequestedParameters();
00280 
00281    // for non block preconditioners see if there are user requested additional parameters
00282    return extraParams_;
00283 }
00284 
00298 bool PreconditionerInverseFactory::updateRequestedParameters(const Teuchos::ParameterList & pl)
00299 {
00300    Teuchos::RCP<BlockPreconditionerFactory> bpf = rcp_dynamic_cast<BlockPreconditionerFactory>(precFactory_);
00301 
00302    // update the parameters of a BPF is required
00303    if(bpf!=Teuchos::null)
00304       return bpf->updateRequestedParameters(pl);
00305 
00306    // for non block preconditioners see if there are user requested additional parameters
00307    if(extraParams_==Teuchos::null)
00308       return true;
00309 
00310    Teuchos::ParameterList::ConstIterator itr;
00311    RCP<Teuchos::ParameterList> srcPl = precFactory_->unsetParameterList();
00312 
00313    // find name of settings sublist
00314    std::string subName = "";
00315    for(itr=srcPl->begin();itr!=srcPl->end();++itr) {
00316       // search for string with "Settings" in name
00317       if(itr->first.find("Settings")!=string::npos) {
00318          subName = itr->first;
00319          continue;
00320       }
00321    }
00322 
00323    // update fails if no settings list was found
00324    if(subName=="") {
00325       precFactory_->setParameterList(srcPl);
00326       return false;
00327    }
00328 
00329    // add extra parameters to list
00330    Teuchos::ParameterList & settingsList = srcPl->sublist(subName);
00331    for(itr=pl.begin();itr!=pl.end();++itr) {
00332       if(extraParams_->isParameter(itr->first))
00333          settingsList.setEntry(itr->first,itr->second);
00334    }
00335 
00336    // set the parameter list
00337    precFactory_->setParameterList(srcPl);
00338 
00339    return true;
00340 }
00341 
00342 void PreconditionerInverseFactory::setupParameterListFromRequestHandler() 
00343 {
00344    // for non block preconditioners see if there are user requested additional parameters
00345    if(extraParams_==Teuchos::null) return;
00346 
00347    Teuchos::ParameterList::ConstIterator itr;
00348    RCP<Teuchos::ParameterList> srcPl = precFactory_->unsetParameterList();
00349 
00350    // find name of settings sublist
00351    std::string subName = "";
00352    for(itr=srcPl->begin();itr!=srcPl->end();++itr) {
00353       // search for string with "Settings" in name
00354       if(itr->first.find("Settings")!=string::npos) {
00355          subName = itr->first;
00356          continue;
00357       }
00358    }
00359 
00360    // update fails if no settings list was found
00361    if(subName=="") {
00362       precFactory_->setParameterList(srcPl);
00363       return;
00364    }
00365 
00366    Teuchos::RCP<Teko::RequestHandler> rh = getRequestHandler();
00367    TEST_FOR_EXCEPTION(rh==Teuchos::null,std::runtime_error,
00368                       "PreconditionerInverseFactory::setupParameterListFromRequestHandler: no request handler set");
00369 
00370    // add extra parameters to list
00371    Teuchos::ParameterList & settingsList = srcPl->sublist(subName);
00372    rh->preRequest<Teuchos::RCP<Teuchos::ParameterList> >(RequestMesg(extraParams_));
00373    Teuchos::RCP<Teuchos::ParameterList> requestParams =
00374          rh->request<Teuchos::RCP<Teuchos::ParameterList> >(RequestMesg(extraParams_));
00375 
00376    TEST_FOR_EXCEPTION(requestParams==Teuchos::null,std::runtime_error,"User specified request not satisfied!");
00377    for(itr=requestParams->begin();itr!=requestParams->end();++itr)
00378       settingsList.setEntry(itr->first,itr->second);
00379 
00380    // reset with updated parameter list
00381    precFactory_->setParameterList(srcPl);
00382 }
00383 
00384 }
 All Classes Files Functions Variables