Teko Version of the Day
Teko_InverseFactory.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_InverseFactory.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 #include "Teko_PreconditionerInverseFactory.hpp"
00109 
00110 using Teuchos::rcp;
00111 using Teuchos::rcp_const_cast;
00112 using Teuchos::rcp_dynamic_cast;
00113 using Teuchos::RCP;
00114 
00115 namespace Teko {
00116 
00118 InverseLinearOp buildInverse(const InverseFactory & factory,const LinearOp & A)
00119 {
00120    InverseLinearOp inv;
00121    try {
00122       inv = factory.buildInverse(A);
00123    }
00124    catch(std::exception & e) {
00125       RCP<Teuchos::FancyOStream> out = Teko::getOutputStream();
00126 
00127       *out << "Teko: \"buildInverse\" could not construct the inverse operator using ";
00128       *out << "\"" << factory.toString() << "\"" << std::endl;
00129       *out << std::endl;
00130       *out << "*** THROWN EXCEPTION ***\n";
00131       *out << e.what() << std::endl;
00132       *out << "************************\n";
00133       
00134       throw e;
00135    }
00136 
00137    return inv;
00138 }
00139 
00151 InverseLinearOp buildInverse(const InverseFactory & factory,const LinearOp & A,const LinearOp & precOp)
00152 {
00153    Teko_DEBUG_SCOPE("buildInverse(factory,A,precOp)",10);
00154    InverseLinearOp inv;
00155    try {
00156       inv = factory.buildInverse(A,precOp);
00157    }
00158    catch(std::exception & e) {
00159       RCP<Teuchos::FancyOStream> out = Teko::getOutputStream();
00160 
00161       *out << "Teko: \"buildInverse\" could not construct the inverse operator using ";
00162       *out << "\"" << factory.toString() << "\"" << std::endl;
00163       *out << std::endl;
00164       *out << "*** THROWN EXCEPTION ***\n";
00165       *out << e.what() << std::endl;
00166       *out << "************************\n";
00167       
00168       throw e;
00169    }
00170 
00171    return inv;
00172 }
00173 
00177 void rebuildInverse(const InverseFactory & factory, const LinearOp & A, InverseLinearOp & invA)
00178 {
00179    InverseLinearOp inv;
00180    try {
00181       factory.rebuildInverse(A,invA);
00182    } 
00183    catch(std::exception & e) {
00184       RCP<Teuchos::FancyOStream> out = Teko::getOutputStream();
00185 
00186       *out << "Teko: \"rebuildInverse\" could not construct the inverse operator using ";
00187       *out << "\"" << factory.toString() << "\"" << std::endl;
00188       *out << std::endl;
00189       *out << "*** THROWN EXCEPTION ***\n";
00190       *out << e.what() << std::endl;
00191       *out << "************************\n";
00192       
00193       throw e;
00194    }
00195 }
00196 
00212 void rebuildInverse(const InverseFactory & factory, const LinearOp & A,const LinearOp & precOp, InverseLinearOp & invA)
00213 {
00214    InverseLinearOp inv;
00215    try {
00216       factory.rebuildInverse(A,precOp,invA);
00217    } 
00218    catch(std::exception & e) {
00219       RCP<Teuchos::FancyOStream> out = Teko::getOutputStream();
00220 
00221       *out << "Teko: \"rebuildInverse\" could not construct the inverse operator using ";
00222       *out << "\"" << factory.toString() << "\"" << std::endl;
00223       *out << std::endl;
00224       *out << "*** THROWN EXCEPTION ***\n";
00225       *out << e.what() << std::endl;
00226       *out << "************************\n";
00227       
00228       throw e;
00229    }
00230 }
00231 
00243 RCP<InverseFactory> invFactoryFromParamList(const Teuchos::ParameterList & list,const std::string & type)
00244 {
00245    RCP<Teuchos::ParameterList> myList = rcp(new Teuchos::ParameterList(list));
00246 
00247    Stratimikos::DefaultLinearSolverBuilder strat;
00248    strat.setParameterList(myList);
00249 
00250    try {
00251       // try to build a preconditioner factory
00252       RCP<Thyra::PreconditionerFactoryBase<double> > precFact = strat.createPreconditioningStrategy(type);
00253 
00254       // string must map to a preconditioner
00255       return rcp(new PreconditionerInverseFactory(precFact,Teuchos::null));
00256    }
00257    catch(const Teuchos::Exceptions::InvalidParameterValue & exp) { }
00258  
00259    try {
00260       // try to build a solver factory
00261       RCP<Thyra::LinearOpWithSolveFactoryBase<double> > solveFact = strat.createLinearSolveStrategy(type);
00262 
00263       // if its around, build a InverseFactory
00264       return rcp(new SolveInverseFactory(solveFact));
00265    }
00266    catch(const Teuchos::Exceptions::InvalidParameterValue & exp) { }
00267 
00268    return  Teuchos::null;;
00269 }
00270 
00279 Teuchos::RCP<const Teuchos::ParameterList> invFactoryValidParameters()
00280 {
00281    Stratimikos::DefaultLinearSolverBuilder strat;
00282  
00283    // extract valid parameter list from Stratimikos
00284    return strat.getValidParameters();
00285 }
00286 
00287 } // end namespace Teko
 All Classes Files Functions Variables