PlayaNewtonArmijoSolverImpl.hpp
Go to the documentation of this file.
00001 /* @HEADER@ */
00002 // ************************************************************************
00003 // 
00004 //                 Playa: Programmable Linear Algebra
00005 //                 Copyright 2012 Sandia Corporation
00006 // 
00007 // Under the terms of Contract DE-AC04-94AL85000 with Sandia Corporation,
00008 // the U.S. Government retains certain rights in this software.
00009 //
00010 // Redistribution and use in source and binary forms, with or without
00011 // modification, are permitted provided that the following conditions are
00012 // met:
00013 //
00014 // 1. Redistributions of source code must retain the above copyright
00015 // notice, this list of conditions and the following disclaimer.
00016 //
00017 // 2. Redistributions in binary form must reproduce the above copyright
00018 // notice, this list of conditions and the following disclaimer in the
00019 // documentation and/or other materials provided with the distribution.
00020 //
00021 // 3. Neither the name of the Corporation nor the names of the
00022 // contributors may be used to endorse or promote products derived from
00023 // this software without specific prior written permission.
00024 //
00025 // THIS SOFTWARE IS PROVIDED BY SANDIA CORPORATION "AS IS" AND ANY
00026 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00027 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
00028 // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SANDIA CORPORATION OR THE
00029 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
00030 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
00031 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
00032 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
00033 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
00034 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
00035 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00036 //
00037 // Questions? Contact Kevin Long (kevin.long@ttu.edu)
00038 // 
00039 
00040 /* @HEADER@ */
00041 
00042 #ifndef PLAYA_NEWTON_ARMIJO_SOLVER_IMPL_HPP
00043 #define PLAYA_NEWTON_ARMIJO_SOLVER_IMPL_HPP
00044 
00045 #include "PlayaNewtonArmijoSolverDecl.hpp"
00046 #include "PlayaNonlinearOperator.hpp"
00047 #include "PlayaTabs.hpp"
00048 #include "PlayaOut.hpp"
00049 #include "Teuchos_ParameterList.hpp"
00050 
00051 #ifndef HAVE_TEUCHOS_EXPLICIT_INSTANTIATION
00052 #include "PlayaLinearCombinationImpl.hpp"
00053 #include "PlayaLinearSolverImpl.hpp"
00054 #include "PlayaLinearOperatorImpl.hpp"
00055 #endif
00056 
00057 
00058 using std::setw;
00059 
00060 namespace Playa
00061 {
00062 
00063 
00064 
00065 template <class Scalar> inline
00066 NewtonArmijoSolver<Scalar>::NewtonArmijoSolver(
00067   const ParameterList& params, 
00068   const LinearSolver<Scalar>& linSolver)
00069     : NonlinearSolverBase<Scalar>(params),
00070       linSolver_(linSolver),
00071       tauR_(10.0*Teuchos::ScalarTraits<Scalar>::eps()),
00072       tauA_(10.0*Teuchos::ScalarTraits<Scalar>::eps()),
00073       alpha_(1.0e-4),
00074       stepReduction_(0.5),
00075       maxIters_(20),
00076       maxLineSearch_(20),
00077       verb_(0)
00078   {
00079     if (params.isParameter("Tau Relative")) tauR_ = params.get<Scalar>("Tau Relative");
00080     if (params.isParameter("Tau Absolute")) tauA_ = params.get<Scalar>("Tau Absolute");
00081     if (params.isParameter("Alpha")) alpha_ = params.get<Scalar>("Alpha");
00082     if (params.isParameter("Step Reduction")) stepReduction_ = params.get<Scalar>("Step Reduction");
00083     if (params.isParameter("Max Iterations")) maxIters_ = params.get<int>("Max Iterations");
00084     if (params.isParameter("Max Backtracks")) maxLineSearch_ = params.get<int>("Max Backtracks");
00085     if (params.isParameter("Verbosity")) verb_ = params.get<int>("Verbosity");
00086   }
00087 
00088 template <class Scalar> inline
00089 SolverState<Scalar> NewtonArmijoSolver<Scalar>::solve(const NonlinearOperator<Scalar>& F,
00090   Vector<Scalar>& soln) const  
00091 {
00092   typedef typename Teuchos::ScalarTraits<Scalar>::magnitudeType ScalarMag;
00093   typedef typename Teuchos::ScalarTraits<Scalar> ST;
00094   
00095   Tabs tab0(0);
00096   PLAYA_MSG1(verb_, tab0 << " begin Playa::NewtonArmijoSolver::solve()");
00097 
00098   soln = F.getInitialGuess().copy();
00099   Vector<Scalar> newtonStep = soln.copy();
00100   
00101   F.setEvalPt(soln);
00102   Vector<Scalar> resid = F.getFunctionValue();
00103 
00104   ScalarMag r0 = resid.norm2();
00105   ScalarMag normF0 = r0;
00106 
00107   for (int i=0; i<maxIters_; i++)
00108   {
00109     Tabs tab1;
00110     PLAYA_MSG2(verb_, tab1 << "Newton iter #" << setw(6) << i << " |F|=" << setw(12) << normF0 << " |F|/|F0|="
00111       << setw(12) << normF0/r0);
00112     
00113     if (normF0 < r0*tauR_ + tauA_)
00114     {
00115       PLAYA_MSG3(verb_, tab1 << "|F|=" << setw(12) << normF0);
00116       PLAYA_MSG3(verb_, tab1 << "Relative tolerance tauR=" << setw(12) << tauR_);
00117       PLAYA_MSG3(verb_, tab1 << "Absolute tolerance tauA=" << setw(12) << tauA_);
00118       PLAYA_MSG3(verb_, tab1 << "  F0*tauR+tauA=" << setw(12) << r0*tauR_ + tauA_);
00119       PLAYA_MSG2(verb_, tab1 << "converged!");
00120       PLAYA_MSG1(verb_, tab0 << " done Playa::NewtonArmijoSolver::solve()");
00121       soln = F.currentEvalPt().copy();
00122       return SolverState<Scalar>(SolveConverged, "NewtonArmijoSolver::solve converged",
00123         i, normF0);
00124     }
00125     LinearOperator<Scalar> J = F.getJacobian();
00126 
00127 
00128     SolverState<Scalar> linSolverState = linSolver_.solve(J, resid, newtonStep);
00129     if (linSolverState.finalState() != SolveConverged)
00130     {
00131       PLAYA_MSG1(verb_, tab0 << " done Playa::NewtonArmijoSolver::solve()");
00132       return SolverState<Scalar>(SolveCrashed, 
00133         "NewtonArmijoSolver::solve: linear solve failed with message [" 
00134         + linSolverState.finalMsg() + "]", i, normF0);
00135     }
00136       
00137     
00138     Scalar t = ST::one();
00139 
00140     bool stepAccepted = false;
00141     soln = F.currentEvalPt().copy();
00142     
00143     for (int j=0; j<maxLineSearch_; j++)
00144     {
00145       Tabs tab2;
00146       Vector<Scalar> tmp = soln - t*newtonStep;
00147       F.setEvalPt( tmp );
00148       resid = F.getFunctionValue();
00149       ScalarMag normF1 = resid.norm2();
00150       PLAYA_MSG2(verb_, tab2 << "step t=" << setw(12) << t << " |F|=" << setw(12) << normF1);
00151       if (normF1 < (ST::one() - alpha_*t)*normF0)
00152       {
00153         stepAccepted = true;
00154         normF0 = normF1;
00155         break;
00156       }
00157       t = stepReduction_*t;
00158     }
00159     
00160     if (!stepAccepted)
00161     {
00162       PLAYA_MSG1(verb_, tab0 << " done Playa::NewtonArmijoSolver::solve()");
00163       return SolverState<Scalar>(SolveCrashed, 
00164         "NewtonArmijoSolver: line search failed",i, normF0);
00165     }
00166   }
00167   
00168   PLAYA_MSG1(verb_, tab0 << " done Playa::NewtonArmijoSolver::solve()");
00169   return SolverState<Scalar>(SolveFailedToConverge, "NewtonArmijoSolver: convergence failure after "
00170     + Teuchos::toString(maxIters_) + " steps.", maxIters_, normF0); 
00171 }
00172 
00173 }
00174 
00175 
00176 #endif

Site Contact