Thyra  Version of the Day
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Thyra_DefaultStateEliminationModelEvaluator.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_DEFAULT_STATE_ELIMINATION_MODEL_EVALUATOR_HPP
11 #define THYRA_DEFAULT_STATE_ELIMINATION_MODEL_EVALUATOR_HPP
12 
13 #include "Thyra_ModelEvaluatorDelegatorBase.hpp"
14 #include "Thyra_DefaultNominalBoundsOverrideModelEvaluator.hpp"
15 #include "Thyra_NonlinearSolverBase.hpp"
16 #include "Teuchos_Time.hpp"
17 
18 
19 namespace Thyra {
20 
21 
29 template<class Scalar>
31  : virtual public ModelEvaluatorDelegatorBase<Scalar>
32 {
33 public:
34 
37 
40 
43  const Teuchos::RCP<ModelEvaluator<Scalar> > &thyraModel,
44  const Teuchos::RCP<NonlinearSolverBase<Scalar> > &stateSolver
45  );
46 
48  void initialize(
49  const Teuchos::RCP<ModelEvaluator<Scalar> > &thyraModel,
50  const Teuchos::RCP<NonlinearSolverBase<Scalar> > &stateSolver
51  );
52 
54  void uninitialize(
55  Teuchos::RCP<ModelEvaluator<Scalar> > *thyraModel = NULL,
56  Teuchos::RCP<NonlinearSolverBase<Scalar> > *stateSolver = NULL
57  );
58 
60 
63 
65  std::string description() const;
66 
68 
71 
88 
90 
91 private:
92 
95 
97  ModelEvaluatorBase::OutArgs<Scalar> createOutArgsImpl() const;
99  void evalModelImpl(
102  ) const;
103 
105 
106 
107 private:
108 
111 
113 
114  mutable Teuchos::RCP<VectorBase<Scalar> > x_guess_solu_;
115 
116 };
117 
118 // /////////////////////////////////
119 // Implementations
120 
121 // Constructors/initializers/accessors/utilities
122 
123 template<class Scalar>
125 {}
126 
127 template<class Scalar>
129  const Teuchos::RCP<ModelEvaluator<Scalar> > &thyraModel
130  ,const Teuchos::RCP<NonlinearSolverBase<Scalar> > &stateSolver
131  )
132 {
133  initialize(thyraModel,stateSolver);
134 }
135 
136 template<class Scalar>
138  const Teuchos::RCP<ModelEvaluator<Scalar> > &thyraModel
139  ,const Teuchos::RCP<NonlinearSolverBase<Scalar> > &stateSolver
140  )
141 {
143  TEUCHOS_TEST_FOR_EXCEPT(!stateSolver.get());
144  stateSolver_ = stateSolver;
145  x_guess_solu_ = Teuchos::null; // We will get the guess at the last possible moment!
146  wrappedThyraModel_ = Teuchos::rcp(
148  Teuchos::rcp_const_cast<ModelEvaluator<Scalar> >(thyraModel)
149  ,Teuchos::null
150  )
151  );
152  stateSolver_->setModel(wrappedThyraModel_);
153 }
154 
155 template<class Scalar>
159  )
160 {
161  if(thyraModel) *thyraModel = this->getUnderlyingModel();
162  if(stateSolver) *stateSolver = stateSolver_;
164  stateSolver_ = Teuchos::null;
165  wrappedThyraModel_ = Teuchos::null;
166  x_guess_solu_ = Teuchos::null;
167 }
168 
169 // Public functions overridden from Teuchos::Describable
170 
171 
172 template<class Scalar>
174 {
176  thyraModel = this->getUnderlyingModel();
177  std::ostringstream oss;
178  oss << "Thyra::DefaultStateEliminationModelEvaluator{";
179  oss << "thyraModel=";
180  if(thyraModel.get())
181  oss << "\'"<<thyraModel->description()<<"\'";
182  else
183  oss << "NULL";
184  oss << ",stateSolver=";
185  if(stateSolver_.get())
186  oss << "\'"<<stateSolver_->description()<<"\'";
187  else
188  oss << "NULL";
189  oss << "}";
190  return oss.str();
191 }
192 
193 // Public functions overridden from ModelEvaulator
194 
195 template<class Scalar>
198 {
199  return Teuchos::null;
200 }
201 
202 template<class Scalar>
205 {
206  return Teuchos::null;
207 }
208 
209 template<class Scalar>
212 {
213  typedef ModelEvaluatorBase MEB;
215  thyraModel = this->getUnderlyingModel();
216  MEB::InArgsSetup<Scalar> nominalValues(thyraModel->getNominalValues());
217  nominalValues.setModelEvalDescription(this->description());
218  nominalValues.setUnsupportsAndRelated(MEB::IN_ARG_x); // Wipe out x, x_dot ...
219  return nominalValues;
220 }
221 
222 template<class Scalar>
225 {
226  typedef ModelEvaluatorBase MEB;
228  thyraModel = this->getUnderlyingModel();
229  MEB::InArgsSetup<Scalar> lowerBounds(thyraModel->getLowerBounds());
230  lowerBounds.setModelEvalDescription(this->description());
231  lowerBounds.setUnsupportsAndRelated(MEB::IN_ARG_x); // Wipe out x, x_dot ...
232  return lowerBounds;
233 }
234 
235 template<class Scalar>
238 {
239  typedef ModelEvaluatorBase MEB;
241  thyraModel = this->getUnderlyingModel();
242  MEB::InArgsSetup<Scalar> upperBounds(thyraModel->getUpperBounds());
243  upperBounds.setModelEvalDescription(this->description());
244  upperBounds.setUnsupportsAndRelated(MEB::IN_ARG_x); // Wipe out x, x_dot ...
245  return upperBounds;
246 }
247 
248 template<class Scalar>
251 {
252  return Teuchos::null;
253 }
254 
255 template<class Scalar>
258 {
259  return Teuchos::null;
260 }
261 
262 
263 template<class Scalar>
266 {
267  typedef ModelEvaluatorBase MEB;
269  thyraModel = this->getUnderlyingModel();
270  const MEB::InArgs<Scalar> wrappedInArgs = thyraModel->createInArgs();
271  MEB::InArgsSetup<Scalar> inArgs;
272  inArgs.setModelEvalDescription(this->description());
273  inArgs.set_Np(wrappedInArgs.Np());
274  inArgs.setSupports(wrappedInArgs);
275  inArgs.setUnsupportsAndRelated(MEB::IN_ARG_x); // Wipe out x, x_dot ...
276  return inArgs;
277 }
278 
279 
280 // Private functions overridden from ModelEvaulatorDefaultBase
281 
282 
283 template<class Scalar>
286 {
287  typedef ModelEvaluatorBase MEB;
289  thyraModel = this->getUnderlyingModel();
290  const MEB::OutArgs<Scalar> wrappedOutArgs = thyraModel->createOutArgs();
291  const int Np = wrappedOutArgs.Np(), Ng = wrappedOutArgs.Ng();
292  MEB::OutArgsSetup<Scalar> outArgs;
293  outArgs.setModelEvalDescription(this->description());
294  outArgs.set_Np_Ng(Np,Ng);
295  outArgs.setSupports(wrappedOutArgs);
296  outArgs.setUnsupportsAndRelated(MEB::IN_ARG_x); // wipe out DgDx ...
297  outArgs.setUnsupportsAndRelated(MEB::OUT_ARG_f); // wipe out f, W, DfDp ...
298  return outArgs;
299 }
300 
301 template<class Scalar>
302 void DefaultStateEliminationModelEvaluator<Scalar>::evalModelImpl(
303  const ModelEvaluatorBase::InArgs<Scalar> &inArgs,
304  const ModelEvaluatorBase::OutArgs<Scalar> &outArgs
305  ) const
306 {
307  typedef ModelEvaluatorBase MEB;
308  using Teuchos::rcp;
309  using Teuchos::rcp_const_cast;
310  using Teuchos::rcp_dynamic_cast;
311  using Teuchos::OSTab;
312  using Teuchos::as;
313 
314  Teuchos::Time totalTimer(""), timer("");
315  totalTimer.start(true);
316 
317  const Teuchos::RCP<Teuchos::FancyOStream> out = this->getOStream();
318  const Teuchos::EVerbosityLevel verbLevel = this->getVerbLevel();
319  Teuchos::OSTab tab(out);
320  if(out.get() && static_cast<int>(verbLevel) >= static_cast<int>(Teuchos::VERB_LOW))
321  *out << "\nEntering Thyra::DefaultStateEliminationModelEvaluator<Scalar>::evalModel(...) ...\n";
322 
324  thyraModel = this->getUnderlyingModel();
325 
326  const int Np = outArgs.Np(), Ng = outArgs.Ng();
327 
328  // Get the intial state guess if not already gotten
329  if (is_null(x_guess_solu_)) {
330  const ModelEvaluatorBase::InArgs<Scalar>
331  nominalValues = thyraModel->getNominalValues();
332  if(nominalValues.get_x().get()) {
333  x_guess_solu_ = nominalValues.get_x()->clone_v();
334  }
335  else {
336  x_guess_solu_ = createMember(thyraModel->get_x_space());
337  assign(x_guess_solu_.ptr(), as<Scalar>(0.0));
338  }
339  }
340 
341  // Reset the nominal values
342  MEB::InArgs<Scalar> wrappedNominalValues = thyraModel->getNominalValues();
343  wrappedNominalValues.setArgs(inArgs,true);
344  wrappedNominalValues.set_x(x_guess_solu_);
345 
347  //VOTSME thyraModel_outputTempState(rcp(&wrappedThyraModel,false),out,verbLevel);
348 
350  VOTSNSB statSolver_outputTempState(
351  stateSolver_,out
352  ,static_cast<int>(verbLevel) >= static_cast<int>(Teuchos::VERB_LOW) ? Teuchos::VERB_LOW : Teuchos::VERB_NONE
353  );
354 
355  if(out.get() && static_cast<int>(verbLevel) >= static_cast<int>(Teuchos::VERB_EXTREME))
356  *out
357  << "\ninArgs =\n" << Teuchos::describe(inArgs,verbLevel)
358  << "\noutArgs on input =\n" << Teuchos::describe(outArgs,Teuchos::VERB_LOW);
359 
360  if(out.get() && static_cast<int>(verbLevel) >= static_cast<int>(Teuchos::VERB_LOW))
361  *out << "\nSolving f(x,...) for x ...\n";
362 
363  wrappedThyraModel_->setNominalValues(
364  rcp(new MEB::InArgs<Scalar>(wrappedNominalValues))
365  );
366 
367  SolveStatus<Scalar> solveStatus = stateSolver_->solve(&*x_guess_solu_,NULL);
368 
369  if( solveStatus.solveStatus == SOLVE_STATUS_CONVERGED ) {
370 
371  if(out.get() && static_cast<int>(verbLevel) >= static_cast<int>(Teuchos::VERB_LOW))
372  *out << "\nComputing the output functions at the solved state solution ...\n";
373 
374  MEB::InArgs<Scalar> wrappedInArgs = thyraModel->createInArgs();
375  MEB::OutArgs<Scalar> wrappedOutArgs = thyraModel->createOutArgs();
376  wrappedInArgs.setArgs(inArgs,true);
377  wrappedInArgs.set_x(x_guess_solu_);
378  wrappedOutArgs.setArgs(outArgs,true);
379 
380  for( int l = 0; l < Np; ++l ) {
381  for( int j = 0; j < Ng; ++j ) {
382  if(
383  outArgs.supports(MEB::OUT_ARG_DgDp,j,l).none()==false
384  && outArgs.get_DgDp(j,l).isEmpty()==false
385  )
386  {
387  // Set DfDp(l) and DgDx(j) to be computed!
388  //wrappedOutArgs.set_DfDp(l,...);
389  //wrappedOutArgs.set_DgDx(j,...);
391  }
392  }
393  }
394 
395  thyraModel->evalModel(wrappedInArgs,wrappedOutArgs);
396 
397  //
398  // Compute DgDp(j,l) using direct sensitivties
399  //
400  for( int l = 0; l < Np; ++l ) {
401  if(
402  wrappedOutArgs.supports(MEB::OUT_ARG_DfDp,l).none()==false
403  && wrappedOutArgs.get_DfDp(l).isEmpty()==false
404  )
405  {
406  //
407  // Compute: D(l) = -inv(DfDx)*DfDp(l)
408  //
410  for( int j = 0; j < Ng; ++j ) {
411  if(
412  outArgs.supports(MEB::OUT_ARG_DgDp,j,l).none()==false
413  && outArgs.get_DgDp(j,l).isEmpty()==false
414  )
415  {
416  //
417  // Compute: DgDp(j,l) = DgDp(j,l) + DgDx(j)*D
418  //
420  }
421  }
422  }
423  }
424  // ToDo: Add a mode to compute DgDp(l) using adjoint sensitivities?
425 
426  }
427  else {
428 
429  if(out.get() && static_cast<int>(verbLevel) >= static_cast<int>(Teuchos::VERB_LOW))
430  *out << "\nFailed to converge, returning NaNs ...\n";
431  outArgs.setFailed();
432 
433  }
434 
435  if(out.get() && static_cast<int>(verbLevel) >= static_cast<int>(Teuchos::VERB_EXTREME))
436  *out
437  << "\noutArgs on output =\n" << Teuchos::describe(outArgs,verbLevel);
438 
439  totalTimer.stop();
440  if(out.get() && static_cast<int>(verbLevel) >= static_cast<int>(Teuchos::VERB_LOW))
441  *out
442  << "\nTotal evaluation time = "<<totalTimer.totalElapsedTime()<<" sec\n"
443  << "\nLeaving Thyra::DefaultStateEliminationModelEvaluator<Scalar>::evalModel(...) ...\n";
444 
445 }
446 
447 
448 } // namespace Thyra
449 
450 
451 #endif // THYRA_DEFAULT_STATE_ELIMINATION_MODEL_EVALUATOR_HPP
void initialize(int *argc, char ***argv)
Pure abstract base interface for evaluating a stateless &quot;model&quot; that can be mapped into a number of d...
bool is_null(const boost::shared_ptr< T > &p)
basic_OSTab< char > OSTab
Concrete aggregate class for all output arguments computable by a ModelEvaluator subclass object...
This is a base class that delegetes almost all function to a wrapped model evaluator object...
void initialize(const RCP< ModelEvaluator< Scalar > > &model)
Initialize given a non-const model evaluator.
Base class for all nonlinear equation solvers.
This class wraps any ModelEvaluator object along with a NonlinearSolverBase object and eliminates the...
T * get() const
Teuchos::RCP< const VectorSpaceBase< Scalar > > get_x_space() const
Teuchos::RCP< LinearOpWithSolveBase< Scalar > > create_W() const
Teuchos::RCP< const VectorSpaceBase< Scalar > > get_f_space() const
TEUCHOS_DEPRECATED RCP< T > rcp(T *p, Dealloc_T dealloc, bool owns_mem)
This class wraps any ModelEvaluator object and allows the client to overide the state contained in th...
void initialize(const Teuchos::RCP< ModelEvaluator< Scalar > > &thyraModel, const Teuchos::RCP< NonlinearSolverBase< Scalar > > &stateSolver)
Base subclass for ModelEvaluator that defines some basic types.
TypeTo as(const TypeFrom &t)
The requested solution criteria has likely been achieved.
#define TEUCHOS_TEST_FOR_EXCEPT(throw_exception_test)
Concrete aggregate class for all input arguments computable by a ModelEvaluator subclass object...