Rythmos - Transient Integration for Differential Equations  Version of the Day
 All Classes Functions Variables Typedefs Pages
Rythmos_BackwardEulerStepper_def.hpp
1 //@HEADER
2 // ***********************************************************************
3 //
4 // Rythmos Package
5 // Copyright (2006) 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 // This library is free software; you can redistribute it and/or modify
11 // it under the terms of the GNU Lesser General Public License as
12 // published by the Free Software Foundation; either version 2.1 of the
13 // License, or (at your option) any later version.
14 //
15 // This library is distributed in the hope that it will be useful, but
16 // WITHOUT ANY WARRANTY; without even the implied warranty of
17 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 // Lesser General Public License for more details.
19 //
20 // You should have received a copy of the GNU Lesser General Public
21 // License along with this library; if not, write to the Free Software
22 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
23 // USA
24 // Questions? Contact Todd S. Coffey (tscoffe@sandia.gov)
25 //
26 // ***********************************************************************
27 //@HEADER
28 
29 #ifndef Rythmos_BACKWARD_EULER_STEPPER_DEF_H
30 #define Rythmos_BACKWARD_EULER_STEPPER_DEF_H
31 
32 #include "Rythmos_BackwardEulerStepper_decl.hpp"
33 #include "Rythmos_DataStore.hpp"
34 #include "Rythmos_LinearInterpolator.hpp"
35 #include "Rythmos_InterpolatorBaseHelpers.hpp"
36 #include "Rythmos_StepperHelpers.hpp"
37 
38 #include "Thyra_ModelEvaluatorHelpers.hpp"
39 #include "Thyra_AssertOp.hpp"
40 #include "Thyra_TestingTools.hpp"
41 
42 #include "Teuchos_VerboseObjectParameterListHelpers.hpp"
43 #include "Teuchos_as.hpp"
44 
45 namespace Rythmos {
46 
47 
48 template<class Scalar>
49 RCP<BackwardEulerStepper<Scalar> >
50 backwardEulerStepper(
51  const RCP<Thyra::ModelEvaluator<Scalar> >& model,
52  const RCP<Thyra::NonlinearSolverBase<Scalar> >& solver
53  )
54 {
55  return Teuchos::rcp(new BackwardEulerStepper<Scalar>(model, solver));
56 }
57 
58 template<class Scalar>
59 RCP<BackwardEulerStepper<Scalar> >
60 backwardEulerStepper()
61 {
62  return Teuchos::rcp(new BackwardEulerStepper<Scalar>());
63 }
64 
65 
66 // ////////////////////////////
67 // Defintions
68 
69 
70 // Constructors, intializers, Misc.
71 
72 
73 template<class Scalar>
75 {
76  typedef Teuchos::ScalarTraits<Scalar> ST;
77  this->defaultInitializeAll_();
78  numSteps_ = 0;
79  dt_ = ST::zero();
80 }
81 
82 
83 template<class Scalar>
85  const RCP<Thyra::ModelEvaluator<Scalar> >& model,
86  const RCP<Thyra::NonlinearSolverBase<Scalar> >& solver
87  )
88 {
89  typedef Teuchos::ScalarTraits<Scalar> ST;
90  this->defaultInitializeAll_();
91  dt_ = ST::zero();
92  numSteps_ = 0;
93  setModel(model);
94  setSolver(solver);
95 }
96 
97 template<class Scalar>
99 {
100  typedef Teuchos::ScalarTraits<Scalar> ST;
101  isInitialized_ = false;
102  haveInitialCondition_ = false;
103  model_ = Teuchos::null;
104  solver_ = Teuchos::null;
105  scaled_x_old_ = Teuchos::null;
106  x_dot_old_ = Teuchos::null;
107  // basePoint_;
108  x_ = Teuchos::null;
109  x_dot_ = Teuchos::null;
110  t_ = ST::nan();
111  t_old_ = ST::nan();
112  dt_ = ST::nan();
113  numSteps_ = -1;
114  neModel_ = Teuchos::null;
115  parameterList_ = Teuchos::null;
116  interpolator_ = Teuchos::null;
117 }
118 
119 // Overridden from InterpolatorAcceptingObjectBase
120 
121 template<class Scalar>
123  const RCP<InterpolatorBase<Scalar> >& interpolator
124  )
125 {
126 #ifdef HAVE_RYTHMOS_DEBUG
127  TEUCHOS_TEST_FOR_EXCEPT(is_null(interpolator));
128 #endif
129  interpolator_ = interpolator;
130  isInitialized_ = false;
131 }
132 
133 template<class Scalar>
134 RCP<InterpolatorBase<Scalar> >
136 {
137  return interpolator_;
138 }
139 
140 template<class Scalar>
141 RCP<const InterpolatorBase<Scalar> >
143 {
144  return interpolator_;
145 }
146 
147 template<class Scalar>
148 RCP<InterpolatorBase<Scalar> >
150 {
151  RCP<InterpolatorBase<Scalar> > temp_interpolator = interpolator_;
152  interpolator_ = Teuchos::null;
153  return(temp_interpolator);
154  isInitialized_ = false;
155 }
156 
157 
158 // Overridden from SolverAcceptingStepperBase
159 
160 
161 template<class Scalar>
163  const RCP<Thyra::NonlinearSolverBase<Scalar> > &solver
164  )
165 {
166  using Teuchos::as;
167 
168  TEUCHOS_TEST_FOR_EXCEPTION(solver == Teuchos::null, std::logic_error,
169  "Error! Thyra::NonlinearSolverBase RCP passed in through BackwardEulerStepper::setSolver is null!"
170  );
171 
172  RCP<Teuchos::FancyOStream> out = this->getOStream();
173  Teuchos::EVerbosityLevel verbLevel = this->getVerbLevel();
174  Teuchos::OSTab ostab(out,1,"BES::setSolver");
175  if ( as<int>(verbLevel) >= as<int>(Teuchos::VERB_HIGH) ) {
176  *out << "solver = " << solver->description() << std::endl;
177  }
178 
179  solver_ = solver;
180 
181  isInitialized_ = false;
182 
183 }
184 
185 
186 template<class Scalar>
187 RCP<Thyra::NonlinearSolverBase<Scalar> >
189 {
190  return solver_;
191 }
192 
193 
194 template<class Scalar>
195 RCP<const Thyra::NonlinearSolverBase<Scalar> >
197 {
198  return solver_;
199 }
200 
201 
202 // Overridden from StepperBase
203 
204 
205 template<class Scalar>
207 {
208  return true;
209 }
210 
211 
212 template<class Scalar>
213 RCP<StepperBase<Scalar> >
215 {
216  RCP<BackwardEulerStepper<Scalar> >
217  stepper = Teuchos::rcp(new BackwardEulerStepper<Scalar>);
218  stepper->isInitialized_ = isInitialized_;
219  stepper->model_ = model_; // Model is stateless so shallow copy is okay!
220  if (!is_null(solver_))
221  stepper->solver_ = solver_->cloneNonlinearSolver().assert_not_null();
222  if (!is_null(x_))
223  stepper->x_ = x_->clone_v().assert_not_null();
224  if (!is_null(scaled_x_old_))
225  stepper->scaled_x_old_ = scaled_x_old_->clone_v().assert_not_null();
226  stepper->t_ = t_;
227  stepper->t_old_ = t_old_;
228  stepper->dt_ = dt_;
229  stepper->numSteps_ = numSteps_;
230  if (!is_null(neModel_))
231  stepper->neModel_
233  if (!is_null(parameterList_))
234  stepper->parameterList_ = parameterList(*parameterList_);
235  if (!is_null(interpolator_))
236  stepper->interpolator_
237  = interpolator_->cloneInterpolator().assert_not_null(); // ToDo: Implement cloneInterpolator()
238  return stepper;
239 }
240 
241 template<class Scalar>
243 {
244  return true;
245 }
246 
247 template<class Scalar>
249  const RCP<const Thyra::ModelEvaluator<Scalar> >& model
250  )
251 {
252 
253  using Teuchos::as;
254 
255  TEUCHOS_TEST_FOR_EXCEPT( is_null(model) );
256  assertValidModel( *this, *model );
257 
258  RCP<Teuchos::FancyOStream> out = this->getOStream();
259  Teuchos::EVerbosityLevel verbLevel = this->getVerbLevel();
260  Teuchos::OSTab ostab(out,1,"BES::setModel");
261  if ( as<int>(verbLevel) >= as<int>(Teuchos::VERB_HIGH) ) {
262  *out << "model = " << model->description() << std::endl;
263  }
264  model_ = model;
265 
266  // Wipe out x. This will either be set thorugh setInitialCondition(...) or
267  // it will be taken from the model's nominal vlaues!
268 // x_ = Teuchos::null;
269 // scaled_x_old_ = Teuchos::null;
270 // x_dot_ = Teuchos::null;
271 // x_dot_old_ = Teuchos::null;
272 
273 // isInitialized_ = false;
274 // haveInitialCondition_ = setDefaultInitialConditionFromNominalValues<Scalar>(
275 // *model_, Teuchos::ptr(this) );
276 
277 }
278 
279 template<class Scalar>
281  const RCP<Thyra::ModelEvaluator<Scalar> >& model
282  )
283 {
284  this->setModel(model); // TODO: 09/09/09 tscoffe: Use ConstNonconstObjectContainer here!
285 }
286 
287 template<class Scalar>
288 RCP<const Thyra::ModelEvaluator<Scalar> >
290 {
291  return model_;
292 }
293 
294 
295 template<class Scalar>
296 RCP<Thyra::ModelEvaluator<Scalar> >
298 {
299  return Teuchos::null;
300 }
301 
302 
303 template<class Scalar>
305  const Thyra::ModelEvaluatorBase::InArgs<Scalar> &initialCondition
306  )
307 {
308 
309  typedef Teuchos::ScalarTraits<Scalar> ST;
310  typedef Thyra::ModelEvaluatorBase MEB;
311 
312  basePoint_ = initialCondition;
313 
314  // x
315 
316  RCP<const Thyra::VectorBase<Scalar> >
317  x_init = initialCondition.get_x();
318 
319 #ifdef HAVE_RYTHMOS_DEBUG
320  TEUCHOS_TEST_FOR_EXCEPTION(
321  is_null(x_init), std::logic_error,
322  "Error, if the client passes in an intial condition to setInitialCondition(...),\n"
323  "then x can not be null!" );
324 #endif
325 
326  x_ = x_init->clone_v();
327 
328  // x_dot
329 
330  RCP<const Thyra::VectorBase<Scalar> >
331  x_dot_init = initialCondition.get_x_dot();
332 
333  if (!is_null(x_dot_init)) {
334  x_dot_ = x_dot_init->clone_v();
335  }
336  else {
337  x_dot_ = createMember(x_->space());
338  assign(x_dot_.ptr(),ST::zero());
339  }
340 
341  // t
342 
343  t_ = initialCondition.get_t();
344 
345  t_old_ = t_;
346 
347  // x_old
348 
349  scaled_x_old_ = x_->clone_v();
350 
351  // x_dot_old
352 
353  x_dot_old_ = x_dot_->clone_v();
354 
355 
356  haveInitialCondition_ = true;
357 
358 }
359 
360 
361 template<class Scalar>
362 Thyra::ModelEvaluatorBase::InArgs<Scalar>
364 {
365  return basePoint_;
366 }
367 
368 
369 template<class Scalar>
370 Scalar BackwardEulerStepper<Scalar>::takeStep(Scalar dt, StepSizeType stepSizeType)
371 {
372 
373  using Teuchos::as;
374  using Teuchos::incrVerbLevel;
375  typedef Teuchos::ScalarTraits<Scalar> ST;
376  typedef Thyra::NonlinearSolverBase<Scalar> NSB;
377  typedef Teuchos::VerboseObjectTempState<NSB> VOTSNSB;
378 
379  initialize();
380 
381  RCP<Teuchos::FancyOStream> out = this->getOStream();
382  Teuchos::EVerbosityLevel verbLevel = this->getVerbLevel();
383  Teuchos::OSTab ostab(out,1,"BES::takeStep");
384  VOTSNSB solver_outputTempState(solver_,out,incrVerbLevel(verbLevel,-1));
385 
386  if ( !is_null(out) && as<int>(verbLevel) >= as<int>(Teuchos::VERB_LOW) ) {
387  *out
388  << "\nEntering " << Teuchos::TypeNameTraits<BackwardEulerStepper<Scalar> >::name()
389  << "::takeStep("<<dt<<","<<toString(stepSizeType)<<") ...\n";
390  }
391 
392  dt_ = dt;
393 
394  if ((stepSizeType == STEP_TYPE_VARIABLE) || (dt == ST::zero())) {
395  if ( as<int>(verbLevel) >= as<int>(Teuchos::VERB_LOW) )
396  *out << "\nThe arguments to takeStep are not valid for BackwardEulerStepper at this time." << std::endl;
397  // print something out about this method not supporting automatic variable step-size
398  return(Scalar(-ST::one()));
399  }
400  if ( as<int>(verbLevel) >= as<int>(Teuchos::VERB_HIGH) ) {
401  *out << "\ndt = " << dt << std::endl;
402  }
403 
404 
405  //
406  // Setup the nonlinear equations:
407  //
408  // f( (1/dt)* x + (-1/dt)*x_old), x, t ) = 0
409  //
410 
411  V_StV( scaled_x_old_.ptr(), Scalar(-ST::one()/dt), *x_ );
412  t_old_ = t_;
413  if(!neModel_.get()) {
414  neModel_ = Teuchos::rcp(new Rythmos::SingleResidualModelEvaluator<Scalar>());
415  }
416  neModel_->initializeSingleResidualModel(
417  model_, basePoint_,
418  Scalar(ST::one()/dt), scaled_x_old_,
419  ST::one(), Teuchos::null,
420  t_old_+dt,
421  Teuchos::null
422  );
423  if( solver_->getModel().get() != neModel_.get() ) {
424  solver_->setModel(neModel_);
425  }
426  // 2007/05/18: rabartl: ToDo: Above, set the stream and the verbosity level
427  // on solver_ so that we an see what it is doing!
428 
429  //
430  // Solve the implicit nonlinear system to a tolerance of ???
431  //
432 
433  if ( as<int>(verbLevel) >= as<int>(Teuchos::VERB_LOW) ) {
434  *out << "\nSolving the implicit backward-Euler timestep equation ...\n";
435  }
436 
437  Thyra::SolveStatus<Scalar>
438  neSolveStatus = solver_->solve(&*x_);
439 
440  // In the above solve, on input *x_ is the old value of x for the previous
441  // time step which is used as the initial guess for the solver. On output,
442  // *x_ is the converged timestep solution.
443 
444  if ( as<int>(verbLevel) >= as<int>(Teuchos::VERB_LOW) ) {
445  *out << "\nOutput status of nonlinear solve:\n" << neSolveStatus;
446  }
447 
448  // 2007/05/18: rabartl: ToDo: Above, get the solve status from the above
449  // solve and at least print warning message if the solve fails! Actually,
450  // you should most likely thrown an exception if the solve fails or return
451  // false if appropriate
452 
453  //
454  // Update the step
455  //
456 
457  assign( x_dot_old_.ptr(), *x_dot_ );
458 
459  // x_dot = (1/dt)*x - (1/dt)*x_old
460  V_StV( x_dot_.ptr(), Scalar(ST::one()/dt), *x_ );
461  Vp_StV( x_dot_.ptr(), Scalar(ST::one()), *scaled_x_old_ );
462 
463  t_ += dt;
464 
465  numSteps_++;
466 
467  if ( as<int>(verbLevel) >= as<int>(Teuchos::VERB_HIGH) ) {
468  *out << "\nt_old_ = " << t_old_ << std::endl;
469  *out << "\nt_ = " << t_ << std::endl;
470  }
471 
472 #ifdef HAVE_RYTHMOS_DEBUG
473  // 04/14/09 tscoffe: This code should be moved to StepperValidator
474 
475  if ( includesVerbLevel(verbLevel,Teuchos::VERB_LOW) )
476  *out << "\nChecking to make sure that solution and the interpolated solution are the same! ...\n";
477 
478  {
479 
480  typedef ScalarTraits<Scalar> ST;
481  typedef ScalarTraits<ScalarMag> SMT;
482 
483  Teuchos::OSTab tab(out);
484 
485  const StepStatus<Scalar> stepStatus = this->getStepStatus();
486 
487  RCP<const Thyra::VectorBase<Scalar> >
488  x = stepStatus.solution,
489  xdot = stepStatus.solutionDot;
490 
491  Array<Scalar> time_vec = Teuchos::tuple(stepStatus.time);
492  Array<RCP<const Thyra::VectorBase<Scalar> > > x_vec, xdot_vec;
493  this->getPoints(time_vec,&x_vec,&xdot_vec,0);
494 
495  RCP<const Thyra::VectorBase<Scalar> >
496  x_interp = x_vec[0],
497  xdot_interp = xdot_vec[0];
498 
499  TEUCHOS_TEST_FOR_EXCEPT(
500  !Thyra::testRelNormDiffErr(
501  "x", *x, "x_interp", *x_interp,
502  "2*epsilon", ScalarMag(100.0*SMT::eps()),
503  "2*epsilon", ScalarMag(100.0*SMT::eps()),
504  includesVerbLevel(verbLevel,Teuchos::VERB_HIGH) ? out.get() : 0
505  )
506  );
507 
508  TEUCHOS_TEST_FOR_EXCEPT(
509  !Thyra::testRelNormDiffErr(
510  "xdot", *xdot, "xdot_interp", *xdot_interp,
511  "2*epsilon", ScalarMag(100.0*SMT::eps()),
512  "2*epsilon", ScalarMag(100.0*SMT::eps()),
513  includesVerbLevel(verbLevel,Teuchos::VERB_HIGH) ? out.get() : 0
514  )
515  );
516 
517  }
518 
519  // 2007/07/25: rabartl: ToDo: Move the above test into a helper function so
520  // that it can be used from lots of different places!
521 
522 #endif // HAVE_RYTHMOS_DEBUG
523 
524  if ( !is_null(out) && as<int>(verbLevel) >= as<int>(Teuchos::VERB_LOW) ) {
525  *out
526  << "\nLeaving " << Teuchos::TypeNameTraits<BackwardEulerStepper<Scalar> >::name()
527  << "::takeStep(...) ...\n";
528  }
529 
530  return(dt);
531 
532 }
533 
534 
535 template<class Scalar>
537 {
538 
539  typedef Teuchos::ScalarTraits<Scalar> ST;
540 
541  StepStatus<Scalar> stepStatus; // Defaults to unknown status
542 
543  if (!isInitialized_) {
544  stepStatus.stepStatus = STEP_STATUS_UNINITIALIZED;
545  }
546  else if (numSteps_ > 0) {
547  stepStatus.stepStatus = STEP_STATUS_CONVERGED;
548  }
549  // else unknown
550 
551  stepStatus.stepSize = dt_;
552  stepStatus.order = 1;
553  stepStatus.time = t_;
554  stepStatus.solution = x_;
555  stepStatus.solutionDot = x_dot_;
556 
557  return(stepStatus);
558 
559 }
560 
561 
562 // Overridden from InterpolationBufferBase
563 
564 
565 template<class Scalar>
566 RCP<const Thyra::VectorSpaceBase<Scalar> >
568 {
569  return ( !is_null(model_) ? model_->get_x_space() : Teuchos::null );
570 }
571 
572 
573 template<class Scalar>
575  const Array<Scalar>& time_vec,
576  const Array<RCP<const Thyra::VectorBase<Scalar> > >& x_vec,
577  const Array<RCP<const Thyra::VectorBase<Scalar> > >& xdot_vec
578  )
579 {
580 
581  typedef Teuchos::ScalarTraits<Scalar> ST;
582  using Teuchos::as;
583 
584  initialize();
585 
586 #ifdef HAVE_RYTHMOS_DEBUG
587  TEUCHOS_TEST_FOR_EXCEPTION(
588  time_vec.size() == 0, std::logic_error,
589  "Error, addPoints called with an empty time_vec array!\n");
590 #endif // HAVE_RYTHMOS_DEBUG
591 
592  RCP<Teuchos::FancyOStream> out = this->getOStream();
593  Teuchos::EVerbosityLevel verbLevel = this->getVerbLevel();
594  Teuchos::OSTab ostab(out,1,"BES::setPoints");
595 
596  if ( as<int>(verbLevel) >= as<int>(Teuchos::VERB_HIGH) ) {
597  *out << "time_vec = " << std::endl;
598  for (int i=0 ; i<Teuchos::as<int>(time_vec.size()) ; ++i) {
599  *out << "time_vec[" << i << "] = " << time_vec[i] << std::endl;
600  }
601  }
602  else if (time_vec.size() == 1) {
603  int n = 0;
604  t_ = time_vec[n];
605  t_old_ = t_;
606  Thyra::V_V(x_.ptr(),*x_vec[n]);
607  Thyra::V_V(scaled_x_old_.ptr(),*x_);
608  }
609  else {
610  int n = time_vec.size()-1;
611  int nm1 = time_vec.size()-2;
612  t_ = time_vec[n];
613  t_old_ = time_vec[nm1];
614  Thyra::V_V(x_.ptr(),*x_vec[n]);
615  Scalar dt = t_ - t_old_;
616  Thyra::V_StV(scaled_x_old_.ptr(),Scalar(-ST::one()/dt),*x_vec[nm1]);
617  }
618 }
619 
620 
621 template<class Scalar>
623 {
624  if ( !isInitialized_ && haveInitialCondition_ )
625  return timeRange<Scalar>(t_,t_);
626  if ( !isInitialized_ && !haveInitialCondition_ )
627  return invalidTimeRange<Scalar>();
628  return timeRange<Scalar>(t_old_,t_);
629 }
630 
631 
632 template<class Scalar>
634  const Array<Scalar>& time_vec,
635  Array<RCP<const Thyra::VectorBase<Scalar> > >* x_vec,
636  Array<RCP<const Thyra::VectorBase<Scalar> > >* xdot_vec,
637  Array<ScalarMag>* accuracy_vec
638  ) const
639 {
640  typedef Teuchos::ScalarTraits<Scalar> ST;
641  using Teuchos::constOptInArg;
642  using Teuchos::ptr;
643 #ifdef HAVE_RYTHMOS_DEBUG
644  TEUCHOS_ASSERT(haveInitialCondition_);
645 #endif // HAVE_RYTHMOS_DEBUG
646  RCP<Thyra::VectorBase<Scalar> > x_temp = x_;
647  if (compareTimeValues(t_old_,t_)!=0) {
648  Scalar dt = t_ - t_old_;
649  x_temp = scaled_x_old_->clone_v();
650  Thyra::Vt_S(x_temp.ptr(),Scalar(-ST::one()*dt)); // undo the scaling
651  }
652  defaultGetPoints<Scalar>(
653  t_old_, constOptInArg(*x_temp), constOptInArg(*x_dot_old_),
654  t_, constOptInArg(*x_), constOptInArg(*x_dot_),
655  time_vec, ptr(x_vec), ptr(xdot_vec), ptr(accuracy_vec),
656  ptr(interpolator_.get())
657  );
658 
659  /*
660  using Teuchos::as;
661  typedef Teuchos::ScalarTraits<Scalar> ST;
662  typedef typename ST::magnitudeType ScalarMag;
663  typedef Teuchos::ScalarTraits<ScalarMag> SMT;
664  typename DataStore<Scalar>::DataStoreVector_t ds_nodes;
665  typename DataStore<Scalar>::DataStoreVector_t ds_out;
666 
667 #ifdef HAVE_RYTHMOS_DEBUG
668  TEUCHOS_TEST_FOR_EXCEPT(!haveInitialCondition_);
669  TEUCHOS_TEST_FOR_EXCEPT( 0 == x_vec );
670 #endif
671 
672  RCP<Teuchos::FancyOStream> out = this->getOStream();
673  Teuchos::EVerbosityLevel verbLevel = this->getVerbLevel();
674  Teuchos::OSTab ostab(out,1,"BES::getPoints");
675  if ( !is_null(out) && as<int>(verbLevel) >= as<int>(Teuchos::VERB_LOW) ) {
676  *out
677  << "\nEntering " << Teuchos::TypeNameTraits<BackwardEulerStepper<Scalar> >::name()
678  << "::getPoints(...) ...\n";
679  }
680  if ( as<int>(verbLevel) >= as<int>(Teuchos::VERB_HIGH) ) {
681  for (int i=0 ; i<Teuchos::as<int>(time_vec.size()) ; ++i) {
682  *out << "time_vec[" << i << "] = " << time_vec[i] << std::endl;
683  }
684  *out << "I can interpolate in the interval [" << t_old_ << "," << t_ << "]." << std::endl;
685  }
686  if (t_old_ != t_) {
687  if ( as<int>(verbLevel) >= as<int>(Teuchos::VERB_HIGH) ) {
688  *out << "Passing two points to interpolator: " << t_old_ << " and " << t_ << std::endl;
689  }
690  DataStore<Scalar> ds_temp;
691  Scalar dt = t_ - t_old_;
692 #ifdef HAVE_RYTHMOS_DEBUG
693  TEUCHOS_TEST_FOR_EXCEPT(
694  !Thyra::testRelErr(
695  "dt", dt, "dt_", dt_,
696  "1e+4*epsilon", ScalarMag(1e+4*SMT::eps()),
697  "1e+2*epsilon", ScalarMag(1e+2*SMT::eps()),
698  as<int>(verbLevel) >= as<int>(Teuchos::VERB_MEDIUM) ? out.get() : 0
699  )
700  );
701 #endif
702  RCP<Thyra::VectorBase<Scalar> >
703  x_temp = scaled_x_old_->clone_v();
704  Thyra::Vt_S(&*x_temp,Scalar(-ST::one()*dt)); // undo the scaling
705  ds_temp.time = t_old_;
706  ds_temp.x = x_temp;
707  ds_temp.xdot = x_dot_old_;
708  ds_temp.accuracy = ScalarMag(dt);
709  ds_nodes.push_back(ds_temp);
710  ds_temp.time = t_;
711  ds_temp.x = x_;
712  ds_temp.xdot = x_dot_;
713  ds_temp.accuracy = ScalarMag(dt);
714  ds_nodes.push_back(ds_temp);
715  }
716  else {
717  if ( as<int>(verbLevel) >= as<int>(Teuchos::VERB_HIGH) ) {
718  *out << "Passing one point to interpolator: " << t_ << std::endl;
719  }
720  DataStore<Scalar> ds_temp;
721  ds_temp.time = t_;
722  ds_temp.x = x_;
723  ds_temp.xdot = x_dot_;
724  ds_temp.accuracy = ScalarMag(ST::zero());
725  ds_nodes.push_back(ds_temp);
726  }
727  interpolate<Scalar>(*interpolator_,rcp(&ds_nodes,false),time_vec,&ds_out);
728  Array<Scalar> time_out;
729  dataStoreVectorToVector(ds_out,&time_out,x_vec,xdot_vec,accuracy_vec);
730  if ( as<int>(verbLevel) >= as<int>(Teuchos::VERB_HIGH) ) {
731  *out << "Passing out the interpolated values:" << std::endl;
732  for (int i=0; i<Teuchos::as<int>(time_out.size()) ; ++i) {
733  *out << "time[" << i << "] = " << time_out[i] << std::endl;
734  *out << "x_vec[" << i << "] = " << std::endl;
735  (*x_vec)[i]->describe(*out,Teuchos::VERB_EXTREME);
736  if (xdot_vec) {
737  if ( (*xdot_vec)[i] == Teuchos::null) {
738  *out << "xdot_vec[" << i << "] = Teuchos::null" << std::endl;
739  }
740  else {
741  *out << "xdot_vec[" << i << "] = " << std::endl;
742  (*xdot_vec)[i]->describe(*out,Teuchos::VERB_EXTREME);
743  }
744  }
745  if(accuracy_vec) {
746  *out << "accuracy[" << i << "] = " << (*accuracy_vec)[i] << std::endl;
747  }
748  }
749  }
750  if ( !is_null(out) && as<int>(verbLevel) >= as<int>(Teuchos::VERB_LOW) ) {
751  *out
752  << "Leaving " << Teuchos::TypeNameTraits<BackwardEulerStepper<Scalar> >::name()
753  << "::getPoints(...) ...\n";
754  }
755  */
756 
757 }
758 
759 
760 template<class Scalar>
761 void BackwardEulerStepper<Scalar>::getNodes(Array<Scalar>* time_vec) const
762 {
763  using Teuchos::as;
764 
765  TEUCHOS_ASSERT( time_vec != NULL );
766 
767  time_vec->clear();
768 
769  if (!haveInitialCondition_) {
770  return;
771  }
772 
773  time_vec->push_back(t_old_);
774 
775  if (numSteps_ > 0) {
776  time_vec->push_back(t_);
777  }
778 
779  RCP<Teuchos::FancyOStream> out = this->getOStream();
780  Teuchos::EVerbosityLevel verbLevel = this->getVerbLevel();
781  Teuchos::OSTab ostab(out,1,"BES::getNodes");
782  if ( as<int>(verbLevel) >= as<int>(Teuchos::VERB_HIGH) ) {
783  *out << this->description() << std::endl;
784  for (int i=0 ; i<Teuchos::as<int>(time_vec->size()) ; ++i) {
785  *out << "time_vec[" << i << "] = " << (*time_vec)[i] << std::endl;
786  }
787  }
788 }
789 
790 
791 template<class Scalar>
792 void BackwardEulerStepper<Scalar>::removeNodes(Array<Scalar>& time_vec)
793 {
794  initialize();
795  using Teuchos::as;
796  RCP<Teuchos::FancyOStream> out = this->getOStream();
797  Teuchos::EVerbosityLevel verbLevel = this->getVerbLevel();
798  Teuchos::OSTab ostab(out,1,"BES::removeNodes");
799  if ( as<int>(verbLevel) >= as<int>(Teuchos::VERB_HIGH) ) {
800  *out << "time_vec = " << std::endl;
801  for (int i=0 ; i<Teuchos::as<int>(time_vec.size()) ; ++i) {
802  *out << "time_vec[" << i << "] = " << time_vec[i] << std::endl;
803  }
804  }
805  TEUCHOS_TEST_FOR_EXCEPTION(true,std::logic_error,"Error, removeNodes is not implemented for BackwardEulerStepper at this time.\n");
806  // TODO:
807  // if any time in time_vec matches t_ or t_old_, then do the following:
808  // remove t_old_: set t_old_ = t_ and set scaled_x_old_ = x_
809  // remove t_: set t_ = t_old_ and set x_ = -dt*scaled_x_old_
810 }
811 
812 
813 template<class Scalar>
815 {
816  return(1);
817 }
818 
819 
820 // Overridden from Teuchos::ParameterListAcceptor
821 
822 
823 template <class Scalar>
825  RCP<Teuchos::ParameterList> const& paramList
826  )
827 {
828  TEUCHOS_TEST_FOR_EXCEPT(is_null(paramList));
829  paramList->validateParametersAndSetDefaults(*this->getValidParameters());
830  parameterList_ = paramList;
831  Teuchos::readVerboseObjectSublist(&*parameterList_,this);
832 }
833 
834 
835 template <class Scalar>
836 RCP<Teuchos::ParameterList>
838 {
839  return(parameterList_);
840 }
841 
842 
843 template <class Scalar>
844 RCP<Teuchos::ParameterList>
846 {
847  RCP<Teuchos::ParameterList>
848  temp_param_list = parameterList_;
849  parameterList_ = Teuchos::null;
850  return(temp_param_list);
851 }
852 
853 
854 template<class Scalar>
855 RCP<const Teuchos::ParameterList>
857 {
858  using Teuchos::ParameterList;
859  static RCP<const ParameterList> validPL;
860  if (is_null(validPL)) {
861  RCP<ParameterList> pl = Teuchos::parameterList();
862  Teuchos::setupVerboseObjectSublist(&*pl);
863  validPL = pl;
864  }
865  return validPL;
866 }
867 
868 
869 // Overridden from Teuchos::Describable
870 
871 
872 template<class Scalar>
874  Teuchos::FancyOStream &out,
875  const Teuchos::EVerbosityLevel verbLevel
876  ) const
877 {
878  using Teuchos::as;
879  Teuchos::OSTab tab(out);
880  if (!isInitialized_) {
881  out << this->description() << " : This stepper is not initialized yet" << std::endl;
882  return;
883  }
884  if (
885  as<int>(verbLevel) == as<int>(Teuchos::VERB_DEFAULT)
886  ||
887  as<int>(verbLevel) >= as<int>(Teuchos::VERB_LOW)
888  )
889  {
890  out << this->description() << "::describe:" << std::endl;
891  out << "model = " << model_->description() << std::endl;
892  out << "solver = " << solver_->description() << std::endl;
893  if (neModel_ == Teuchos::null) {
894  out << "neModel = Teuchos::null" << std::endl;
895  } else {
896  out << "neModel = " << neModel_->description() << std::endl;
897  }
898  }
899  else if (as<int>(verbLevel) >= as<int>(Teuchos::VERB_LOW)) {
900  out << "t_ = " << t_ << std::endl;
901  out << "t_old_ = " << t_old_ << std::endl;
902  }
903  else if (as<int>(verbLevel) >= as<int>(Teuchos::VERB_MEDIUM)) {
904  }
905  else if (as<int>(verbLevel) >= as<int>(Teuchos::VERB_HIGH)) {
906  out << "model_ = " << std::endl;
907  model_->describe(out,verbLevel);
908  out << "solver_ = " << std::endl;
909  solver_->describe(out,verbLevel);
910  if (neModel_ == Teuchos::null) {
911  out << "neModel = Teuchos::null" << std::endl;
912  } else {
913  out << "neModel = " << std::endl;
914  neModel_->describe(out,verbLevel);
915  }
916  out << "x_ = " << std::endl;
917  x_->describe(out,verbLevel);
918  out << "scaled_x_old_ = " << std::endl;
919  scaled_x_old_->describe(out,verbLevel);
920  }
921 }
922 
923 
924 // private
925 
926 
927 template <class Scalar>
929 {
930 
931  if (isInitialized_)
932  return;
933 
934  TEUCHOS_TEST_FOR_EXCEPT(is_null(model_));
935  TEUCHOS_TEST_FOR_EXCEPT(is_null(solver_));
936  TEUCHOS_TEST_FOR_EXCEPT(!haveInitialCondition_);
937 
938 #ifdef HAVE_RYTHMOS_DEBUG
939  THYRA_ASSERT_VEC_SPACES(
940  "Rythmos::BackwardEulerStepper::initialize(...)",
941  *x_->space(), *model_->get_x_space() );
942 #endif // HAVE_RYTHMOS_DEBUG
943 
944  if ( is_null(interpolator_) ) {
945  // If an interpolator has not been explicitly set, then just create
946  // a default linear interpolator.
947  interpolator_ = linearInterpolator<Scalar>();
948  // 2007/05/18: rabartl: ToDo: Replace this with a Hermete interplator
949  // when it is implementated!
950  }
951 
952  isInitialized_ = true;
953 
954 }
955 
956 template<class Scalar>
957 RCP<const MomentoBase<Scalar> >
959 {
960  RCP<BackwardEulerStepperMomento<Scalar> > momento = Teuchos::rcp(new BackwardEulerStepperMomento<Scalar>());
961  momento->set_scaled_x_old(scaled_x_old_);
962  momento->set_x_dot_old(x_dot_old_);
963  momento->set_x(x_);
964  momento->set_x_dot(x_dot_);
965  momento->set_t(t_);
966  momento->set_t_old(t_old_);
967  momento->set_dt(dt_);
968  momento->set_numSteps(numSteps_);
969  momento->set_isInitialized(isInitialized_);
970  momento->set_haveInitialCondition(haveInitialCondition_);
971  momento->set_parameterList(parameterList_);
972  momento->set_basePoint(basePoint_);
973  momento->set_neModel(neModel_);
974  momento->set_interpolator(interpolator_);
975  return momento;
976 }
977 
978 template<class Scalar>
980  const Ptr<const MomentoBase<Scalar> >& momentoPtr,
981  const RCP<Thyra::ModelEvaluator<Scalar> >& model,
982  const RCP<Thyra::NonlinearSolverBase<Scalar> >& solver
983  )
984 {
985  Ptr<const BackwardEulerStepperMomento<Scalar> > feMomentoPtr =
986  Teuchos::ptr_dynamic_cast<const BackwardEulerStepperMomento<Scalar> >(momentoPtr,true);
987  const BackwardEulerStepperMomento<Scalar>& feMomento = *feMomentoPtr;
988  model_ = model;
989  solver_ = solver;
990  scaled_x_old_ = feMomento.get_scaled_x_old();
991  x_dot_old_ = feMomento.get_x_dot_old();
992  x_ = feMomento.get_x();
993  x_dot_ = feMomento.get_x_dot();
994  t_ = feMomento.get_t();
995  t_old_ = feMomento.get_t_old();
996  dt_ = feMomento.get_dt();
997  numSteps_ = feMomento.get_numSteps();
998  isInitialized_ = feMomento.get_isInitialized();
999  haveInitialCondition_ = feMomento.get_haveInitialCondition();
1000  parameterList_ = feMomento.get_parameterList();
1001  basePoint_ = feMomento.get_basePoint();
1002  neModel_ = feMomento.get_neModel();
1003  interpolator_ = feMomento.get_interpolator();
1004  this->checkConsistentState_();
1005 }
1006 
1007 template<class Scalar>
1009 {
1010  if (isInitialized_) {
1011  TEUCHOS_ASSERT( !Teuchos::is_null(model_) );
1012  TEUCHOS_ASSERT( !Teuchos::is_null(solver_) );
1013  TEUCHOS_ASSERT( haveInitialCondition_ );
1014  TEUCHOS_ASSERT( !Teuchos::is_null(interpolator_) );
1015  }
1016  if (haveInitialCondition_) {
1017  // basePoint_ should be defined
1018  typedef Teuchos::ScalarTraits<Scalar> ST;
1019  TEUCHOS_ASSERT( !ST::isnaninf(t_) );
1020  TEUCHOS_ASSERT( !ST::isnaninf(t_old_) );
1021  TEUCHOS_ASSERT( !Teuchos::is_null(scaled_x_old_) );
1022  TEUCHOS_ASSERT( !Teuchos::is_null(x_dot_old_) );
1023  TEUCHOS_ASSERT( !Teuchos::is_null(x_) );
1024  TEUCHOS_ASSERT( !Teuchos::is_null(x_dot_) );
1025  TEUCHOS_ASSERT( t_ >= basePoint_.get_t() );
1026  TEUCHOS_ASSERT( t_old_ >= basePoint_.get_t() );
1027  }
1028  if (numSteps_ > 0) {
1029  TEUCHOS_ASSERT(isInitialized_);
1030  }
1031 }
1032 
1033 
1034 //
1035 // Explicit Instantiation macro
1036 //
1037 // Must be expanded from within the Rythmos namespace!
1038 //
1039 
1040 #define RYTHMOS_BACKWARD_EULER_STEPPER_INSTANT(SCALAR) \
1041  \
1042  template class BackwardEulerStepper< SCALAR >; \
1043  \
1044  template RCP< BackwardEulerStepper< SCALAR > > \
1045  backwardEulerStepper( \
1046  const RCP<Thyra::ModelEvaluator< SCALAR > >& model, \
1047  const RCP<Thyra::NonlinearSolverBase< SCALAR > >& solver \
1048  ); \
1049  template RCP< BackwardEulerStepper< SCALAR > > \
1050  backwardEulerStepper();
1051 
1052 
1053 
1054 
1055 } // namespace Rythmos
1056 
1057 #endif //Rythmos_BACKWARD_EULER_STEPPER_DEF_H
Simple concrete stepper subclass implementing an implicit backward Euler method.
Base strategy class for interpolation functionality.
RCP< const InterpolatorBase< Scalar > > getInterpolator() const
void setSolver(const RCP< Thyra::NonlinearSolverBase< Scalar > > &solver)
void getNodes(Array< Scalar > *time_vec) const
RCP< Teuchos::ParameterList > unsetParameterList()
void setInitialCondition(const Thyra::ModelEvaluatorBase::InArgs< Scalar > &initialCondition)
void setModel(const RCP< const Thyra::ModelEvaluator< Scalar > > &model)
void removeNodes(Array< Scalar > &time_vec)
Scalar takeStep(Scalar dt, StepSizeType flag)
void addPoints(const Array< Scalar > &time_vec, const Array< RCP< const Thyra::VectorBase< Scalar > > > &x_vec, const Array< RCP< const Thyra::VectorBase< Scalar > > > &xdot_vec)
RCP< Teuchos::ParameterList > getNonconstParameterList()
RCP< const Thyra::VectorSpaceBase< Scalar > > get_x_space() const
void setNonconstModel(const RCP< Thyra::ModelEvaluator< Scalar > > &model)
RCP< InterpolatorBase< Scalar > > unSetInterpolator()
Thyra::ModelEvaluatorBase::InArgs< Scalar > getInitialCondition() const
Teuchos::ScalarTraits< Scalar >::magnitudeType ScalarMag
void getPoints(const Array< Scalar > &time_vec, Array< RCP< const Thyra::VectorBase< Scalar > > > *x_vec, Array< RCP< const Thyra::VectorBase< Scalar > > > *xdot_vec, Array< ScalarMag > *accuracy_vec) const
RCP< const Thyra::VectorBase< Scalar > > solutionDot
void setMomento(const Ptr< const MomentoBase< Scalar > > &momentoPtr, const RCP< Thyra::ModelEvaluator< Scalar > > &model, const RCP< Thyra::NonlinearSolverBase< Scalar > > &solver)
Set momento object for use in restarts.
void describe(Teuchos::FancyOStream &out, const Teuchos::EVerbosityLevel verbLevel) const
Concrete momento class for the BackwardEulerStepper.
RCP< Thyra::NonlinearSolverBase< Scalar > > getNonconstSolver()
RCP< const MomentoBase< Scalar > > getMomento() const
Get momento object for use in restarts.
Decorator subclass for a steady-state version of a DAE for single-residual time stepper methods...
RCP< const Thyra::ModelEvaluator< Scalar > > getModel() const
RCP< const Teuchos::ParameterList > getValidParameters() const
RCP< StepperBase< Scalar > > cloneStepperAlgorithm() const
Creates copies of all internal data (including the parameter list) except the model which is assumed ...
const StepStatus< Scalar > getStepStatus() const
RCP< const Thyra::VectorBase< Scalar > > solution
RCP< const Thyra::NonlinearSolverBase< Scalar > > getSolver() const
RCP< InterpolatorBase< Scalar > > getNonconstInterpolator()
void setParameterList(RCP< Teuchos::ParameterList > const &paramList)
RCP< Thyra::ModelEvaluator< Scalar > > getNonconstModel()
Base class for a momento object.
void setInterpolator(const RCP< InterpolatorBase< Scalar > > &interpolator)