Thyra Version of the Day
Loading...
Searching...
No Matches
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
21namespace Thyra {
22
23
32template <class Scalar>
81
82
87template <class Scalar>
92
93
94// ////////////////////////
95// Defintions
96
97
98// Overridden from Teuchos::ParameterListAcceptor
99
100
101template<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
118template<class Scalar>
124
125
126template<class Scalar>
129{
130 RCP<Teuchos::ParameterList> _paramList = paramList_;
131 paramList_ = Teuchos::null;
132 return _paramList;
133}
134
135
136template<class Scalar>
139{
140 return paramList_;
141}
142
143
144template<class Scalar>
147{
148 using Teuchos::setDoubleParameter; using Teuchos::setIntParameter;
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
164template <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
175template <class Scalar>
178{
179 return model_;
180}
181
182
183template <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;
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
271template <class Scalar>
274{
275 if (forceUpToDate) {
276 TEUCHOS_TEST_FOR_EXCEPT(forceUpToDate);
277 }
278 return J_;
279}
280
281
282template <class Scalar>
285{
286 return J_;
287}
288
289
290} // namespace Thyra
291
292
293#endif // THYRA_LINEAR_NONLINEAR_SOLVER_BASE_HPP
Ptr< T > ptr() const
T * get() const
Concrete nonlinear solver for linear equations.
RCP< const ModelEvaluator< Scalar > > getModel() const
RCP< LinearOpWithSolveBase< Scalar > > get_nonconst_W(const bool forceUpToDate)
RCP< const LinearOpWithSolveBase< Scalar > > get_W() const
void setParameterList(RCP< Teuchos::ParameterList > const &paramList)
void setModel(const RCP< const ModelEvaluator< Scalar > > &model)
RCP< Teuchos::ParameterList > getNonconstParameterList()
RCP< const Teuchos::ParameterList > getValidParameters() const
RCP< Teuchos::ParameterList > unsetParameterList()
RCP< const Teuchos::ParameterList > getParameterList() const
RCP< LinearNonlinearSolver< Scalar > > linearNonlinearSolver()
SolveStatus< Scalar > solve(VectorBase< Scalar > *x, const SolveCriteria< Scalar > *solveCriteria, VectorBase< Scalar > *delta)
Base class for all linear operators that can support a high-level solve operation.
Base subclass for ModelEvaluator that defines some basic types.
Pure abstract base interface for evaluating a stateless "model" that can be mapped into a number of d...
Base class for all nonlinear equation solvers.
Abstract interface for finite-dimensional dense vectors.
virtual RCP< const VectorSpaceBase< Scalar > > space() const =0
Return a smart pointer to the vector space that this vector belongs to.
#define TEUCHOS_TEST_FOR_EXCEPT(throw_exception_test)
#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.
@ NOTRANS
Use the non-transposed operator.
TypeTo as(const TypeFrom &t)
T_To & dyn_cast(T_From &from)
#define TEUCHOS_OSTAB
TEUCHOS_DEPRECATED RCP< T > rcp(T *p, Dealloc_T dealloc, bool owns_mem)
TEUCHOSCORE_LIB_DLL_EXPORT EVerbosityLevel incrVerbLevel(const EVerbosityLevel inputVerbLevel, const int numLevels)
Simple struct that defines the requested solution criteria for a solve.
Simple struct for the return status from a solve.