Thyra Version of the Day
Thyra_LinearNonlinearSolver.hpp
00001 // @HEADER
00002 // ***********************************************************************
00003 // 
00004 //    Thyra: Interfaces and Support for Abstract Numerical Algorithms
00005 //                 Copyright (2004) Sandia Corporation
00006 // 
00007 // Under terms of Contract DE-AC04-94AL85000, there is a non-exclusive
00008 // license for use of this work by or on behalf of the U.S. Government.
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 Roscoe A. Bartlett (bartlettra@ornl.gov) 
00038 // 
00039 // ***********************************************************************
00040 // @HEADER
00041 
00042 #ifndef THYRA_LINEAR_NONLINEAR_SOLVER_BASE_HPP
00043 #define THYRA_LINEAR_NONLINEAR_SOLVER_BASE_HPP
00044 
00045 
00046 #include "Thyra_NonlinearSolverBase.hpp"
00047 #include "Thyra_ModelEvaluatorHelpers.hpp"
00048 #include "Teuchos_VerboseObjectParameterListHelpers.hpp"
00049 #include "Teuchos_StandardParameterEntryValidators.hpp"
00050 #include "Teuchos_as.hpp"
00051 
00052 
00053 namespace Thyra {
00054 
00055 
00064 template <class Scalar>
00065 class LinearNonlinearSolver : public NonlinearSolverBase<Scalar> {
00066 public:
00067 
00070 
00072   void setParameterList(RCP<Teuchos::ParameterList> const& paramList);
00074   RCP<Teuchos::ParameterList> getNonconstParameterList();
00076   RCP<Teuchos::ParameterList> unsetParameterList();
00078   RCP<const Teuchos::ParameterList> getParameterList() const;
00080   RCP<const Teuchos::ParameterList> getValidParameters() const;
00081 
00083 
00086 
00088   void setModel(
00089     const RCP<const ModelEvaluator<Scalar> > &model
00090     );
00092   RCP<const ModelEvaluator<Scalar> > getModel() const;
00094   SolveStatus<Scalar> solve(
00095     VectorBase<Scalar> *x,
00096     const SolveCriteria<Scalar> *solveCriteria,
00097     VectorBase<Scalar> *delta
00098     );
00100   RCP<LinearOpWithSolveBase<Scalar> > get_nonconst_W(const bool forceUpToDate);
00102   RCP<const LinearOpWithSolveBase<Scalar> > get_W() const;
00103 
00105 
00106 private:
00107 
00108   RCP<Teuchos::ParameterList> paramList_;
00109   RCP<const ModelEvaluator<Scalar> > model_;
00110   RCP<LinearOpWithSolveBase<Scalar> > J_;
00111 
00112 };
00113 
00114 
00119 template <class Scalar>
00120 RCP<LinearNonlinearSolver<Scalar> > linearNonlinearSolver()
00121 {
00122   return Teuchos::rcp(new LinearNonlinearSolver<Scalar>());
00123 }
00124 
00125 
00126 // ////////////////////////
00127 // Defintions
00128 
00129 
00130 // Overridden from Teuchos::ParameterListAcceptor
00131 
00132 
00133 template<class Scalar>
00134 void LinearNonlinearSolver<Scalar>::setParameterList(
00135   RCP<Teuchos::ParameterList> const& paramList
00136   )
00137 {
00138   using Teuchos::get;
00139   TEUCHOS_TEST_FOR_EXCEPT(is_null(paramList));
00140   paramList->validateParametersAndSetDefaults(*getValidParameters(),0);
00141   paramList_ = paramList;
00142   // ToDo: Accept some parameters if this makes sense!
00143   Teuchos::readVerboseObjectSublist(&*paramList_,this);
00144 #ifdef TEUCHOS_DEBUG
00145   paramList_->validateParameters(*getValidParameters(),0);
00146 #endif // TEUCHOS_DEBUG
00147 }
00148 
00149 
00150 template<class Scalar>
00151 RCP<Teuchos::ParameterList>
00152 LinearNonlinearSolver<Scalar>::getNonconstParameterList()
00153 {
00154   return paramList_;
00155 }
00156 
00157 
00158 template<class Scalar>
00159 RCP<Teuchos::ParameterList>
00160 LinearNonlinearSolver<Scalar>::unsetParameterList()
00161 {
00162   RCP<Teuchos::ParameterList> _paramList = paramList_;
00163   paramList_ = Teuchos::null;
00164   return _paramList;
00165 }
00166 
00167 
00168 template<class Scalar>
00169 RCP<const Teuchos::ParameterList>
00170 LinearNonlinearSolver<Scalar>::getParameterList() const
00171 {
00172   return paramList_;
00173 }
00174 
00175 
00176 template<class Scalar>
00177 RCP<const Teuchos::ParameterList>
00178 LinearNonlinearSolver<Scalar>::getValidParameters() const
00179 {
00180   using Teuchos::setDoubleParameter; using Teuchos::setIntParameter;
00181   static RCP<const Teuchos::ParameterList> validPL;
00182   if (is_null(validPL)) {
00183     RCP<Teuchos::ParameterList>
00184       pl = Teuchos::parameterList();
00185     // ToDo: Set up some parameters when needed!
00186     Teuchos::setupVerboseObjectSublist(&*pl);
00187     validPL = pl;
00188   }
00189   return validPL;
00190 }
00191 
00192 
00193 // Overridden from NonlinearSolverBase
00194 
00195 
00196 template <class Scalar>
00197 void LinearNonlinearSolver<Scalar>::setModel(
00198   const RCP<const ModelEvaluator<Scalar> > &model
00199   )
00200 {
00201   TEUCHOS_TEST_FOR_EXCEPT(model.get()==NULL);
00202   model_ = model;
00203   J_ = Teuchos::null;
00204 }
00205 
00206 
00207 template <class Scalar>
00208 RCP<const ModelEvaluator<Scalar> >
00209 LinearNonlinearSolver<Scalar>::getModel() const
00210 {
00211   return model_;
00212 }
00213 
00214 
00215 template <class Scalar>
00216 SolveStatus<Scalar> LinearNonlinearSolver<Scalar>::solve(
00217   VectorBase<Scalar> *x,
00218   const SolveCriteria<Scalar> *solveCriteria,
00219   VectorBase<Scalar> *delta
00220   )
00221 {
00222 
00223   using std::endl;
00224   using Teuchos::incrVerbLevel;
00225   using Teuchos::describe;
00226   using Teuchos::as;
00227   using Teuchos::rcp;
00228   using Teuchos::OSTab;
00229   using Teuchos::getFancyOStream;
00230   typedef Teuchos::ScalarTraits<Scalar> ST;
00231   typedef Thyra::ModelEvaluatorBase MEB;
00232   typedef Teuchos::VerboseObjectTempState<MEB> VOTSME;
00233   typedef Thyra::LinearOpWithSolveBase<Scalar> LOWSB;
00234   typedef Teuchos::VerboseObjectTempState<LOWSB> VOTSLOWSB;
00235 
00236 #ifdef TEUCHOS_DEBUG
00237   TEUCHOS_TEST_FOR_EXCEPT(0==x);
00238   THYRA_ASSERT_VEC_SPACES(
00239     "TimeStepNonlinearSolver<Scalar>::solve(...)",
00240     *x->space(),*model_->get_x_space() );
00241   TEUCHOS_TEST_FOR_EXCEPT(
00242     0!=solveCriteria && "ToDo: Support passed in solve criteria!" );
00243 #endif
00244   
00245   const RCP<Teuchos::FancyOStream> out = this->getOStream();
00246   const Teuchos::EVerbosityLevel verbLevel = this->getVerbLevel();
00247   const bool showTrace = (as<int>(verbLevel) >= as<int>(Teuchos::VERB_LOW));
00248   const bool dumpAll = (as<int>(verbLevel) == as<int>(Teuchos::VERB_EXTREME)); 
00249   TEUCHOS_OSTAB;
00250   VOTSME stateModel_outputTempState(model_,out,incrVerbLevel(verbLevel,-1));
00251   if(out.get() && showTrace)
00252     *out
00253       << "\nEntering LinearNonlinearSolver::solve(...) ...\n"
00254       << "\nmodel = " << describe(*model_,verbLevel);
00255 
00256   if(out.get() && dumpAll) {
00257     *out << "\nInitial guess:\n";
00258     *out << "\nx = " << *x;
00259   }
00260 
00261   // Compute the Jacobian and the residual at the input point!
00262   if(!J_.get()) J_ = model_->create_W();
00263   RCP<VectorBase<Scalar> >
00264     f = createMember(model_->get_f_space());
00265   if(out.get() && showTrace)
00266     *out << "\nEvaluating the model f and W ...\n";
00267   eval_f_W( *model_, *x,  &*f, &*J_ );
00268 
00269   // Solve the system: J*dx = -f
00270   RCP<VectorBase<Scalar> >
00271     dx = createMember(model_->get_x_space());
00272   if(out.get() && showTrace)
00273     *out << "\nSolving the system J*dx = -f ...\n";
00274   VOTSLOWSB J_outputTempState(J_,out,incrVerbLevel(verbLevel,-1));
00275   assign( dx.ptr(), ST::zero() );
00276   Thyra::SolveStatus<Scalar>
00277     linearSolveStatus = J_->solve(NOTRANS, *f, dx.ptr() );
00278   if(out.get() && showTrace)
00279     *out << "\nLinear solve status:\n" << linearSolveStatus;
00280   Vt_S( dx.ptr(), Scalar(-ST::one()) );
00281   if(out.get() && dumpAll)
00282     *out << "\ndx = " << Teuchos::describe(*dx,verbLevel);
00283   if (delta != NULL) {
00284     Thyra::assign( ptr(delta), *dx );
00285     if(out.get() && dumpAll)
00286       *out << "\ndelta = " << Teuchos::describe(*delta,verbLevel);
00287   }
00288 
00289   // Update the solution: x += dx
00290   Vp_V( ptr(x), *dx );
00291   if(out.get() && dumpAll)
00292     *out << "\nUpdated solution x = " << Teuchos::describe(*x,verbLevel);
00293   
00294   if(out.get() && showTrace)
00295     *out << "\nLeaving LinearNonlinearSolver::solve(...) ...\n";
00296   
00297   // Return default status
00298   return SolveStatus<Scalar>();
00299 
00300 }
00301 
00302 
00303 template <class Scalar>
00304 RCP<LinearOpWithSolveBase<Scalar> >
00305 LinearNonlinearSolver<Scalar>::get_nonconst_W(const bool forceUpToDate)
00306 {
00307   if (forceUpToDate) {
00308     TEUCHOS_TEST_FOR_EXCEPT(forceUpToDate);
00309   }
00310   return J_;
00311 }
00312 
00313 
00314 template <class Scalar>
00315 RCP<const LinearOpWithSolveBase<Scalar> >
00316 LinearNonlinearSolver<Scalar>::get_W() const
00317 {
00318   return J_;
00319 }
00320 
00321 
00322 } // namespace Thyra
00323 
00324 
00325 #endif // THYRA_LINEAR_NONLINEAR_SOLVER_BASE_HPP
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines