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 // Thyra: Interfaces and Support for Abstract Numerical Algorithms
4 //
5 // Copyright 2004 NTESS and the Thyra contributors.
6 // SPDX-License-Identifier: BSD-3-Clause
7 // *****************************************************************************
8 // @HEADER
9 
10 #ifndef THYRA_LINEAR_NONLINEAR_SOLVER_BASE_HPP
11 #define THYRA_LINEAR_NONLINEAR_SOLVER_BASE_HPP
12 
13 
14 #include "Thyra_NonlinearSolverBase.hpp"
15 #include "Thyra_ModelEvaluatorHelpers.hpp"
16 #include "Teuchos_VerboseObjectParameterListHelpers.hpp"
17 #include "Teuchos_StandardParameterEntryValidators.hpp"
18 #include "Teuchos_as.hpp"
19 
20 
21 namespace Thyra {
22 
23 
32 template <class Scalar>
34 public:
35 
38 
40  void setParameterList(RCP<Teuchos::ParameterList> const& paramList);
49 
51 
54 
56  void setModel(
57  const RCP<const ModelEvaluator<Scalar> > &model
58  );
64  const SolveCriteria<Scalar> *solveCriteria,
65  VectorBase<Scalar> *delta
66  );
68  RCP<LinearOpWithSolveBase<Scalar> > get_nonconst_W(const bool forceUpToDate);
71 
73 
74 private:
75 
76  RCP<Teuchos::ParameterList> paramList_;
79 
80 };
81 
82 
87 template <class Scalar>
89 {
91 }
92 
93 
94 // ////////////////////////
95 // Defintions
96 
97 
98 // Overridden from Teuchos::ParameterListAcceptor
99 
100 
101 template<class Scalar>
103  RCP<Teuchos::ParameterList> const& paramList
104  )
105 {
106  using Teuchos::get;
107  TEUCHOS_TEST_FOR_EXCEPT(is_null(paramList));
108  paramList->validateParametersAndSetDefaults(*getValidParameters(),0);
109  paramList_ = paramList;
110  // ToDo: Accept some parameters if this makes sense!
111  Teuchos::readVerboseObjectSublist(&*paramList_,this);
112 #ifdef TEUCHOS_DEBUG
113  paramList_->validateParameters(*getValidParameters(),0);
114 #endif // TEUCHOS_DEBUG
115 }
116 
117 
118 template<class Scalar>
121 {
122  return paramList_;
123 }
124 
125 
126 template<class Scalar>
129 {
130  RCP<Teuchos::ParameterList> _paramList = paramList_;
131  paramList_ = Teuchos::null;
132  return _paramList;
133 }
134 
135 
136 template<class Scalar>
139 {
140  return paramList_;
141 }
142 
143 
144 template<class Scalar>
147 {
148  using Teuchos::setDoubleParameter; using Teuchos::setIntParameter;
149  static RCP<const Teuchos::ParameterList> validPL;
150  if (is_null(validPL)) {
152  pl = Teuchos::parameterList();
153  // ToDo: Set up some parameters when needed!
154  Teuchos::setupVerboseObjectSublist(&*pl);
155  validPL = pl;
156  }
157  return validPL;
158 }
159 
160 
161 // Overridden from NonlinearSolverBase
162 
163 
164 template <class Scalar>
166  const RCP<const ModelEvaluator<Scalar> > &model
167  )
168 {
169  TEUCHOS_TEST_FOR_EXCEPT(model.get()==NULL);
170  model_ = model;
171  J_ = Teuchos::null;
172 }
173 
174 
175 template <class Scalar>
178 {
179  return model_;
180 }
181 
182 
183 template <class Scalar>
186  const SolveCriteria<Scalar> *solveCriteria,
187  VectorBase<Scalar> *delta
188  )
189 {
190 
191  using std::endl;
193  using Teuchos::describe;
194  using Teuchos::as;
195  using Teuchos::rcp;
196  using Teuchos::OSTab;
197  using Teuchos::getFancyOStream;
199  typedef Thyra::ModelEvaluatorBase MEB;
202  typedef Teuchos::VerboseObjectTempState<LOWSB> VOTSLOWSB;
203 
204 #ifdef TEUCHOS_DEBUG
207  "TimeStepNonlinearSolver<Scalar>::solve(...)",
208  *x->space(),*model_->get_x_space() );
210  0!=solveCriteria && "ToDo: Support passed in solve criteria!" );
211 #endif
212 
213  const RCP<Teuchos::FancyOStream> out = this->getOStream();
214  const Teuchos::EVerbosityLevel verbLevel = this->getVerbLevel();
215  const bool showTrace = (as<int>(verbLevel) >= as<int>(Teuchos::VERB_LOW));
216  const bool dumpAll = (as<int>(verbLevel) == as<int>(Teuchos::VERB_EXTREME));
218  VOTSME stateModel_outputTempState(model_,out,incrVerbLevel(verbLevel,-1));
219  if(out.get() && showTrace)
220  *out
221  << "\nEntering LinearNonlinearSolver::solve(...) ...\n"
222  << "\nmodel = " << describe(*model_,verbLevel);
223 
224  if(out.get() && dumpAll) {
225  *out << "\nInitial guess:\n";
226  *out << "\nx = " << *x;
227  }
228 
229  // Compute the Jacobian and the residual at the input point!
230  if(!J_.get()) J_ = model_->create_W();
232  f = createMember(model_->get_f_space());
233  if(out.get() && showTrace)
234  *out << "\nEvaluating the model f and W ...\n";
235  eval_f_W( *model_, *x, &*f, &*J_ );
236 
237  // Solve the system: J*dx = -f
239  dx = createMember(model_->get_x_space());
240  if(out.get() && showTrace)
241  *out << "\nSolving the system J*dx = -f ...\n";
242  VOTSLOWSB J_outputTempState(J_,out,incrVerbLevel(verbLevel,-1));
243  assign( dx.ptr(), ST::zero() );
245  linearSolveStatus = J_->solve(NOTRANS, *f, dx.ptr() );
246  if(out.get() && showTrace)
247  *out << "\nLinear solve status:\n" << linearSolveStatus;
248  Vt_S( dx.ptr(), Scalar(-ST::one()) );
249  if(out.get() && dumpAll)
250  *out << "\ndx = " << Teuchos::describe(*dx,verbLevel);
251  if (delta != NULL) {
252  Thyra::assign( ptr(delta), *dx );
253  if(out.get() && dumpAll)
254  *out << "\ndelta = " << Teuchos::describe(*delta,verbLevel);
255  }
256 
257  // Update the solution: x += dx
258  Vp_V( ptr(x), *dx );
259  if(out.get() && dumpAll)
260  *out << "\nUpdated solution x = " << Teuchos::describe(*x,verbLevel);
261 
262  if(out.get() && showTrace)
263  *out << "\nLeaving LinearNonlinearSolver::solve(...) ...\n";
264 
265  // Return default status
266  return SolveStatus<Scalar>();
267 
268 }
269 
270 
271 template <class Scalar>
274 {
275  if (forceUpToDate) {
276  TEUCHOS_TEST_FOR_EXCEPT(forceUpToDate);
277  }
278  return J_;
279 }
280 
281 
282 template <class Scalar>
285 {
286  return J_;
287 }
288 
289 
290 } // namespace Thyra
291 
292 
293 #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