Thyra  Version of the Day
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Thyra_LinearNonlinearSolver.hpp
1 // @HEADER
2 // ***********************************************************************
3 //
4 // Thyra: Interfaces and Support for Abstract Numerical Algorithms
5 // Copyright (2004) Sandia Corporation
6 //
7 // Under terms of Contract DE-AC04-94AL85000, there is a non-exclusive
8 // license for use of this work by or on behalf of the U.S. Government.
9 //
10 // Redistribution and use in source and binary forms, with or without
11 // modification, are permitted provided that the following conditions are
12 // met:
13 //
14 // 1. Redistributions of source code must retain the above copyright
15 // notice, this list of conditions and the following disclaimer.
16 //
17 // 2. Redistributions in binary form must reproduce the above copyright
18 // notice, this list of conditions and the following disclaimer in the
19 // documentation and/or other materials provided with the distribution.
20 //
21 // 3. Neither the name of the Corporation nor the names of the
22 // contributors may be used to endorse or promote products derived from
23 // this software without specific prior written permission.
24 //
25 // THIS SOFTWARE IS PROVIDED BY SANDIA CORPORATION "AS IS" AND ANY
26 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
28 // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SANDIA CORPORATION OR THE
29 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
30 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
31 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
32 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
33 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
34 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
35 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 //
37 // Questions? Contact Roscoe A. Bartlett (bartlettra@ornl.gov)
38 //
39 // ***********************************************************************
40 // @HEADER
41 
42 #ifndef THYRA_LINEAR_NONLINEAR_SOLVER_BASE_HPP
43 #define THYRA_LINEAR_NONLINEAR_SOLVER_BASE_HPP
44 
45 
46 #include "Thyra_NonlinearSolverBase.hpp"
47 #include "Thyra_ModelEvaluatorHelpers.hpp"
48 #include "Teuchos_VerboseObjectParameterListHelpers.hpp"
49 #include "Teuchos_StandardParameterEntryValidators.hpp"
50 #include "Teuchos_as.hpp"
51 
52 
53 namespace Thyra {
54 
55 
64 template <class Scalar>
66 public:
67 
70 
72  void setParameterList(RCP<Teuchos::ParameterList> const& paramList);
81 
83 
86 
88  void setModel(
89  const RCP<const ModelEvaluator<Scalar> > &model
90  );
96  const SolveCriteria<Scalar> *solveCriteria,
97  VectorBase<Scalar> *delta
98  );
100  RCP<LinearOpWithSolveBase<Scalar> > get_nonconst_W(const bool forceUpToDate);
103 
105 
106 private:
107 
108  RCP<Teuchos::ParameterList> paramList_;
111 
112 };
113 
114 
119 template <class Scalar>
121 {
123 }
124 
125 
126 // ////////////////////////
127 // Defintions
128 
129 
130 // Overridden from Teuchos::ParameterListAcceptor
131 
132 
133 template<class Scalar>
135  RCP<Teuchos::ParameterList> const& paramList
136  )
137 {
138  using Teuchos::get;
139  TEUCHOS_TEST_FOR_EXCEPT(is_null(paramList));
140  paramList->validateParametersAndSetDefaults(*getValidParameters(),0);
141  paramList_ = paramList;
142  // ToDo: Accept some parameters if this makes sense!
143  Teuchos::readVerboseObjectSublist(&*paramList_,this);
144 #ifdef TEUCHOS_DEBUG
145  paramList_->validateParameters(*getValidParameters(),0);
146 #endif // TEUCHOS_DEBUG
147 }
148 
149 
150 template<class Scalar>
153 {
154  return paramList_;
155 }
156 
157 
158 template<class Scalar>
161 {
162  RCP<Teuchos::ParameterList> _paramList = paramList_;
163  paramList_ = Teuchos::null;
164  return _paramList;
165 }
166 
167 
168 template<class Scalar>
171 {
172  return paramList_;
173 }
174 
175 
176 template<class Scalar>
179 {
180  using Teuchos::setDoubleParameter; using Teuchos::setIntParameter;
181  static RCP<const Teuchos::ParameterList> validPL;
182  if (is_null(validPL)) {
184  pl = Teuchos::parameterList();
185  // ToDo: Set up some parameters when needed!
186  Teuchos::setupVerboseObjectSublist(&*pl);
187  validPL = pl;
188  }
189  return validPL;
190 }
191 
192 
193 // Overridden from NonlinearSolverBase
194 
195 
196 template <class Scalar>
198  const RCP<const ModelEvaluator<Scalar> > &model
199  )
200 {
201  TEUCHOS_TEST_FOR_EXCEPT(model.get()==NULL);
202  model_ = model;
203  J_ = Teuchos::null;
204 }
205 
206 
207 template <class Scalar>
210 {
211  return model_;
212 }
213 
214 
215 template <class Scalar>
218  const SolveCriteria<Scalar> *solveCriteria,
219  VectorBase<Scalar> *delta
220  )
221 {
222 
223  using std::endl;
225  using Teuchos::describe;
226  using Teuchos::as;
227  using Teuchos::rcp;
228  using Teuchos::OSTab;
229  using Teuchos::getFancyOStream;
231  typedef Thyra::ModelEvaluatorBase MEB;
234  typedef Teuchos::VerboseObjectTempState<LOWSB> VOTSLOWSB;
235 
236 #ifdef TEUCHOS_DEBUG
239  "TimeStepNonlinearSolver<Scalar>::solve(...)",
240  *x->space(),*model_->get_x_space() );
242  0!=solveCriteria && "ToDo: Support passed in solve criteria!" );
243 #endif
244 
245  const RCP<Teuchos::FancyOStream> out = this->getOStream();
246  const Teuchos::EVerbosityLevel verbLevel = this->getVerbLevel();
247  const bool showTrace = (as<int>(verbLevel) >= as<int>(Teuchos::VERB_LOW));
248  const bool dumpAll = (as<int>(verbLevel) == as<int>(Teuchos::VERB_EXTREME));
250  VOTSME stateModel_outputTempState(model_,out,incrVerbLevel(verbLevel,-1));
251  if(out.get() && showTrace)
252  *out
253  << "\nEntering LinearNonlinearSolver::solve(...) ...\n"
254  << "\nmodel = " << describe(*model_,verbLevel);
255 
256  if(out.get() && dumpAll) {
257  *out << "\nInitial guess:\n";
258  *out << "\nx = " << *x;
259  }
260 
261  // Compute the Jacobian and the residual at the input point!
262  if(!J_.get()) J_ = model_->create_W();
264  f = createMember(model_->get_f_space());
265  if(out.get() && showTrace)
266  *out << "\nEvaluating the model f and W ...\n";
267  eval_f_W( *model_, *x, &*f, &*J_ );
268 
269  // Solve the system: J*dx = -f
271  dx = createMember(model_->get_x_space());
272  if(out.get() && showTrace)
273  *out << "\nSolving the system J*dx = -f ...\n";
274  VOTSLOWSB J_outputTempState(J_,out,incrVerbLevel(verbLevel,-1));
275  assign( dx.ptr(), ST::zero() );
277  linearSolveStatus = J_->solve(NOTRANS, *f, dx.ptr() );
278  if(out.get() && showTrace)
279  *out << "\nLinear solve status:\n" << linearSolveStatus;
280  Vt_S( dx.ptr(), Scalar(-ST::one()) );
281  if(out.get() && dumpAll)
282  *out << "\ndx = " << Teuchos::describe(*dx,verbLevel);
283  if (delta != NULL) {
284  Thyra::assign( ptr(delta), *dx );
285  if(out.get() && dumpAll)
286  *out << "\ndelta = " << Teuchos::describe(*delta,verbLevel);
287  }
288 
289  // Update the solution: x += dx
290  Vp_V( ptr(x), *dx );
291  if(out.get() && dumpAll)
292  *out << "\nUpdated solution x = " << Teuchos::describe(*x,verbLevel);
293 
294  if(out.get() && showTrace)
295  *out << "\nLeaving LinearNonlinearSolver::solve(...) ...\n";
296 
297  // Return default status
298  return SolveStatus<Scalar>();
299 
300 }
301 
302 
303 template <class Scalar>
306 {
307  if (forceUpToDate) {
308  TEUCHOS_TEST_FOR_EXCEPT(forceUpToDate);
309  }
310  return J_;
311 }
312 
313 
314 template <class Scalar>
317 {
318  return J_;
319 }
320 
321 
322 } // namespace Thyra
323 
324 
325 #endif // THYRA_LINEAR_NONLINEAR_SOLVER_BASE_HPP
virtual RCP< const VectorSpaceBase< Scalar > > space() const =0
Return a smart pointer to the vector space that this vector belongs to.
Pure abstract base interface for evaluating a stateless &quot;model&quot; that can be mapped into a number of d...
#define THYRA_ASSERT_VEC_SPACES(FUNC_NAME, VS1, VS2)
This is a very useful macro that should be used to validate that two vector spaces are compatible...
Base class for all linear operators that can support a high-level solve operation.
bool is_null(const boost::shared_ptr< T > &p)
basic_OSTab< char > OSTab
Use the non-transposed operator.
RCP< Teuchos::ParameterList > unsetParameterList()
Base class for all nonlinear equation solvers.
void setParameterList(RCP< Teuchos::ParameterList > const &paramList)
RCP< const Teuchos::ParameterList > getParameterList() const
T * get() const
Concrete nonlinear solver for linear equations.
void setModel(const RCP< const ModelEvaluator< Scalar > > &model)
TEUCHOS_DEPRECATED RCP< T > rcp(T *p, Dealloc_T dealloc, bool owns_mem)
Ptr< T > ptr() const
Abstract interface for finite-dimensional dense vectors.
void validateParametersAndSetDefaults(ParameterList const &validParamList, int const depth=1000)
Simple struct for the return status from a solve.
RCP< LinearOpWithSolveBase< Scalar > > get_nonconst_W(const bool forceUpToDate)
RCP< const ModelEvaluator< Scalar > > getModel() const
Base subclass for ModelEvaluator that defines some basic types.
#define TEUCHOS_OSTAB
TEUCHOSCORE_LIB_DLL_EXPORT EVerbosityLevel incrVerbLevel(const EVerbosityLevel inputVerbLevel, const int numLevels)
RCP< const LinearOpWithSolveBase< Scalar > > get_W() const
TypeTo as(const TypeFrom &t)
SolveStatus< Scalar > solve(VectorBase< Scalar > *x, const SolveCriteria< Scalar > *solveCriteria, VectorBase< Scalar > *delta)
RCP< Teuchos::ParameterList > getNonconstParameterList()
Simple struct that defines the requested solution criteria for a solve.
RCP< LinearNonlinearSolver< Scalar > > linearNonlinearSolver()
#define TEUCHOS_TEST_FOR_EXCEPT(throw_exception_test)
RCP< const Teuchos::ParameterList > getValidParameters() const