Rythmos - Transient Integration for Differential Equations  Version of the Day
 All Classes Functions Variables Typedefs Pages
Rythmos_DefaultIntegrator_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_DEFAULT_INTEGRATOR_DEF_HPP
30 #define RYTHMOS_DEFAULT_INTEGRATOR_DEF_HPP
31 
32 #include "Rythmos_DefaultIntegrator_decl.hpp"
33 #include "Rythmos_InterpolationBufferHelpers.hpp"
34 #include "Rythmos_IntegrationControlStrategyBase.hpp"
35 #include "Rythmos_IntegrationObserverBase.hpp"
36 #include "Rythmos_InterpolationBufferAppenderBase.hpp"
37 #include "Rythmos_PointwiseInterpolationBufferAppender.hpp"
38 #include "Rythmos_StepperHelpers.hpp"
39 #include "Teuchos_VerboseObjectParameterListHelpers.hpp"
40 #include "Teuchos_Assert.hpp"
41 #include "Teuchos_as.hpp"
42 #include <limits>
43 
44 namespace Rythmos {
45 
50 template<class Scalar>
51 RCP<DefaultIntegrator<Scalar> >
53 {
54  RCP<DefaultIntegrator<Scalar> >
55  integrator = Teuchos::rcp(new DefaultIntegrator<Scalar>());
56  return integrator;
57 }
58 
59 
64 template<class Scalar>
65 RCP<DefaultIntegrator<Scalar> >
67  const RCP<IntegrationControlStrategyBase<Scalar> > &integrationControlStrategy,
68  const RCP<IntegrationObserverBase<Scalar> > &integrationObserver
69  )
70 {
71  RCP<DefaultIntegrator<Scalar> >
72  integrator = Teuchos::rcp(new DefaultIntegrator<Scalar>());
73  integrator->setIntegrationControlStrategy(integrationControlStrategy);
74  integrator->setIntegrationObserver(integrationObserver);
75  return integrator;
76 }
77 
78 
83 template<class Scalar>
84 RCP<DefaultIntegrator<Scalar> >
86  const RCP<IntegrationControlStrategyBase<Scalar> > &integrationControlStrategy
87  )
88 {
89  RCP<DefaultIntegrator<Scalar> >
90  integrator = Teuchos::rcp(new DefaultIntegrator<Scalar>());
91  integrator->setIntegrationControlStrategy(integrationControlStrategy);
92  return integrator;
93 }
94 
95 
100 template<class Scalar>
101 RCP<DefaultIntegrator<Scalar> >
103  const RCP<IntegrationObserverBase<Scalar> > &integrationObserver
104  )
105 {
106  RCP<DefaultIntegrator<Scalar> >
107  integrator = Teuchos::rcp(new DefaultIntegrator<Scalar>());
108  integrator->setIntegrationObserver(integrationObserver);
109  return integrator;
110 }
111 
112 
113 
114 // ////////////////////////////
115 // Defintions
116 
117 
118 // Static data members
119 
120 
121 template<class Scalar>
122 const std::string
123 DefaultIntegrator<Scalar>::maxNumTimeSteps_name_ = "Max Number Time Steps";
124 
125 // replaced in code with std::numeric_limits<int>::max()
126 // template<class Scalar>
127 // const int
128 // DefaultIntegrator<Scalar>::maxNumTimeSteps_default_ = 10000;
129 
130 
131 // Constructors, Initializers, Misc
132 
133 
134 template<class Scalar>
136  :landOnFinalTime_(true),
137  maxNumTimeSteps_(std::numeric_limits<int>::max()),
138  currTimeStepIndex_(-1)
139 {}
140 
141 
142 template<class Scalar>
144  const RCP<IntegrationControlStrategyBase<Scalar> > &integrationControlStrategy
145  )
146 {
147 #ifdef HAVE_RYTHMOS_DEBUG
148  TEUCHOS_TEST_FOR_EXCEPT(is_null(integrationControlStrategy));
149 #endif
150  integrationControlStrategy_ = integrationControlStrategy;
151 }
152 
153 template<class Scalar>
154 RCP<IntegrationControlStrategyBase<Scalar> >
156 {
157  return integrationControlStrategy_;
158 }
159 
160 template<class Scalar>
161 RCP<const IntegrationControlStrategyBase<Scalar> >
163 {
164  return integrationControlStrategy_;
165 }
166 
167 
168 template<class Scalar>
170  const RCP<IntegrationObserverBase<Scalar> > &integrationObserver
171  )
172 {
173 #ifdef HAVE_RYTHMOS_DEBUG
174  TEUCHOS_TEST_FOR_EXCEPT(is_null(integrationObserver));
175 #endif
176  integrationObserver_ = integrationObserver;
177 }
178 
179 
180 template<class Scalar>
182  const RCP<InterpolationBufferAppenderBase<Scalar> > &interpBufferAppender
183  )
184 {
185  interpBufferAppender_ = interpBufferAppender.assert_not_null();
186 }
187 
188 
189 template<class Scalar>
190 RCP<const InterpolationBufferAppenderBase<Scalar> >
192 {
193  return interpBufferAppender_;
194 }
195 
196 template<class Scalar>
197 RCP<InterpolationBufferAppenderBase<Scalar> >
199 {
200  return interpBufferAppender_;
201 }
202 
203 template<class Scalar>
204 RCP<InterpolationBufferAppenderBase<Scalar> >
206 {
207  RCP<InterpolationBufferAppenderBase<Scalar> > InterpBufferAppender;
208  std::swap( InterpBufferAppender, interpBufferAppender_ );
209  return InterpBufferAppender;
210 }
211 
212 
213 // Overridden from ParameterListAcceptor
214 
215 
216 template<class Scalar>
218  RCP<ParameterList> const& paramList
219  )
220 {
221  TEUCHOS_TEST_FOR_EXCEPT(is_null(paramList));
222  paramList->validateParameters(*getValidParameters());
223  this->setMyParamList(paramList);
224  maxNumTimeSteps_ = paramList->get(
225  maxNumTimeSteps_name_, std::numeric_limits<int>::max());
226  Teuchos::readVerboseObjectSublist(&*paramList,this);
227 }
228 
229 
230 template<class Scalar>
231 RCP<const ParameterList>
233 {
234  static RCP<const ParameterList> validPL;
235  if (is_null(validPL) ) {
236  RCP<ParameterList> pl = Teuchos::parameterList();
237  pl->set(maxNumTimeSteps_name_, std::numeric_limits<int>::max(),
238  "Set the maximum number of integration time-steps allowed."
239  );
240  Teuchos::setupVerboseObjectSublist(&*pl);
241  validPL = pl;
242  }
243  return validPL;
244 }
245 
246 
247 // Overridden from IntegratorBase
248 
249 
250 template<class Scalar>
251 RCP<IntegratorBase<Scalar> >
253 {
254 
255  using Teuchos::null;
256  RCP<DefaultIntegrator<Scalar> >
257  newIntegrator = Teuchos::rcp(new DefaultIntegrator<Scalar>());
258  // Only copy control information, not the stepper or the model it contains!
259  newIntegrator->stepper_ = Teuchos::null;
260  const RCP<const ParameterList> paramList = this->getParameterList();
261  if (!is_null(paramList)) {
262  newIntegrator->setParameterList(Teuchos::parameterList(*paramList));
263  }
264  if (!is_null(integrationControlStrategy_)) {
265  newIntegrator->integrationControlStrategy_ =
266  integrationControlStrategy_->cloneIntegrationControlStrategy().assert_not_null();
267  }
268  if (!is_null(integrationObserver_)) {
269  newIntegrator->integrationObserver_ =
270  integrationObserver_->cloneIntegrationObserver().assert_not_null();
271  }
272  if (!is_null(trailingInterpBuffer_)) {
273  // ToDo: implement the clone!
274  newIntegrator->trailingInterpBuffer_ = null;
275  //newIntegrator->trailingInterpBuffer_ =
276  // trailingInterpBuffer_->cloneInterploationBuffer().assert_not_null();
277  }
278  if (!is_null(interpBufferAppender_)) {
279  // ToDo: implement the clone!
280  newIntegrator->interpBufferAppender_ = null;
281  //newIntegrator->interpBufferAppender_ =
282  // interpBufferAppender_->cloneInterpolationBufferAppender().assert_not_null();
283  }
284  return newIntegrator;
285 }
286 
287 
288 template<class Scalar>
290  const RCP<StepperBase<Scalar> > &stepper,
291  const Scalar &finalTime,
292  const bool landOnFinalTime
293  )
294 {
295  typedef Teuchos::ScalarTraits<Scalar> ST;
296  TEUCHOS_TEST_FOR_EXCEPT(is_null(stepper));
297  TEUCHOS_TEST_FOR_EXCEPT( finalTime < stepper->getTimeRange().lower() );
298  TEUCHOS_ASSERT( stepper->getTimeRange().length() == ST::zero() );
299  // 2007/07/25: rabartl: ToDo: Validate state of the stepper!
300  stepper_ = stepper;
301  integrationTimeDomain_ = timeRange(stepper_->getTimeRange().lower(), finalTime);
302  landOnFinalTime_ = landOnFinalTime;
303  currTimeStepIndex_ = 0;
304  stepCtrlInfoLast_ = StepControlInfo<Scalar>();
305  if (!is_null(integrationControlStrategy_))
306  integrationControlStrategy_->resetIntegrationControlStrategy(
307  integrationTimeDomain_
308  );
309  if (!is_null(integrationObserver_))
310  integrationObserver_->resetIntegrationObserver(
311  integrationTimeDomain_
312  );
313 }
314 
315 
316 template<class Scalar>
317 RCP<StepperBase<Scalar> > DefaultIntegrator<Scalar>::unSetStepper()
318 {
319  RCP<StepperBase<Scalar> > stepper_temp = stepper_;
320  stepper_ = Teuchos::null;
321  return(stepper_temp);
322 }
323 
324 
325 template<class Scalar>
326 RCP<const StepperBase<Scalar> > DefaultIntegrator<Scalar>::getStepper() const
327 {
328  return(stepper_);
329 }
330 
331 
332 template<class Scalar>
333 RCP<StepperBase<Scalar> > DefaultIntegrator<Scalar>::getNonconstStepper() const
334 {
335  return(stepper_);
336 }
337 
338 
339 template<class Scalar>
341  const RCP<InterpolationBufferBase<Scalar> > &trailingInterpBuffer
342  )
343 {
344  trailingInterpBuffer_ = trailingInterpBuffer.assert_not_null();
345 }
346 
347 
348 template<class Scalar>
349 RCP<InterpolationBufferBase<Scalar> >
351 {
352  return trailingInterpBuffer_;
353 }
354 
355 
356 template<class Scalar>
357 RCP<const InterpolationBufferBase<Scalar> >
359 {
360  return trailingInterpBuffer_;
361 }
362 
363 template<class Scalar>
364 RCP<InterpolationBufferBase<Scalar> >
366 {
367  RCP<InterpolationBufferBase<Scalar> > trailingInterpBuffer;
368  std::swap( trailingInterpBuffer, trailingInterpBuffer_ );
369  return trailingInterpBuffer;
370 }
371 
372 
373 template<class Scalar>
375  const Array<Scalar>& time_vec,
376  Array<RCP<const Thyra::VectorBase<Scalar> > >* x_vec,
377  Array<RCP<const Thyra::VectorBase<Scalar> > >* xdot_vec,
378  Array<ScalarMag>* accuracy_vec
379  )
380 {
381 
382  RYTHMOS_FUNC_TIME_MONITOR_DIFF("Rythmos:DefaultIntegrator::getFwdPoints",
383  TopLevel);
384 
385  using Teuchos::incrVerbLevel;
386 #ifndef _MSC_VER
387  using Teuchos::Describable;
388 #endif
389  typedef Teuchos::ScalarTraits<Scalar> ST;
391  typedef Teuchos::VerboseObjectTempState<IBB> VOTSIBB;
392 
393  finalizeSetup();
394 
395  RCP<Teuchos::FancyOStream> out = this->getOStream();
396  Teuchos::EVerbosityLevel verbLevel = this->getVerbLevel();
397  Teuchos::OSTab tab(out);
398  VOTSIBB stepper_outputTempState(stepper_,out,incrVerbLevel(verbLevel,-1));
399 
400  if ( includesVerbLevel(verbLevel,Teuchos::VERB_LOW) )
401  *out << "\nEntering " << this->Describable::description() << "::getFwdPoints(...) ...\n"
402  << "\nStepper: " << Teuchos::describe(*stepper_,verbLevel);
403 
404  if ( includesVerbLevel(verbLevel,Teuchos::VERB_MEDIUM) )
405  *out << "\nRequested time points: " << Teuchos::toString(time_vec) << "\n";
406 
407  // Observe start of a time integration
408  if (!is_null(integrationObserver_)) {
409  integrationObserver_->setOStream(out);
410  integrationObserver_->setVerbLevel(incrVerbLevel(verbLevel,-1));
411  integrationObserver_->observeStartTimeIntegration(*stepper_);
412  }
413 
414  //
415  // 0) Initial setup
416  //
417 
418  const int numTimePoints = time_vec.size();
419 
420  // Assert preconditions
421  assertTimePointsAreSorted(time_vec);
422  TEUCHOS_TEST_FOR_EXCEPT(accuracy_vec!=0); // ToDo: Remove accuracy_vec!
423 
424  // Resize the storage for the output arrays
425  if (x_vec)
426  x_vec->resize(numTimePoints);
427  if (xdot_vec)
428  xdot_vec->resize(numTimePoints);
429 
430  // This int records the next time point offset in time_vec[timePointIndex]
431  // that needs to be handled. This gets updated as the time points are
432  // filled below.
433  int nextTimePointIndex = 0;
434 
435  assertNoTimePointsBeforeCurrentTimeRange(*this,time_vec,nextTimePointIndex);
436 
437  //
438  // 1) First, get all time points that fall within the current time range
439  //
440 
441  {
442  RYTHMOS_FUNC_TIME_MONITOR("Rythmos:DefaultIntegrator::getFwdPoints: getPoints");
443  // 2007/10/05: rabartl: ToDo: Get points from trailingInterpBuffer_ first!
444  getCurrentPoints(*stepper_,time_vec,x_vec,xdot_vec,&nextTimePointIndex);
445  }
446 
447  //
448  // 2) Advance the stepper to satisfy time points in time_vec that fall
449  // before the current time.
450  //
451 
452  while ( nextTimePointIndex < numTimePoints ) {
453 
454  // Use the time stepping algorithm to step up to or past the next
455  // requested time but not so far as to step past the point entirely.
456  const Scalar t = time_vec[nextTimePointIndex];
457  bool advanceStepperToTimeSucceeded = false;
458  {
459  RYTHMOS_FUNC_TIME_MONITOR("Rythmos:DefaultIntegrator::getFwdPoints: advanceStepperToTime");
460  advanceStepperToTimeSucceeded= advanceStepperToTime(t);
461  }
462  if (!advanceStepperToTimeSucceeded) {
463  bool reachedMaxNumTimeSteps = (currTimeStepIndex_ >= maxNumTimeSteps_);
464  if (reachedMaxNumTimeSteps) {
465  // Break out of the while loop and attempt to exit gracefully.
466  break;
467  }
468  TEUCHOS_TEST_FOR_EXCEPTION(
469  !advanceStepperToTimeSucceeded, Exceptions::GetFwdPointsFailed,
470  this->description() << "\n\n"
471  "Error: The integration failed to get to time " << t << " and only achieved\n"
472  "getting to " << stepper_->getTimeRange().upper() << "!"
473  );
474  }
475 
476  // Extract the next set of points (perhaps just one) from the stepper
477  {
478  RYTHMOS_FUNC_TIME_MONITOR("Rythmos:DefaultIntegrator::getFwdPoints: getPoints (fwd)");
479  getCurrentPoints(*stepper_,time_vec,x_vec,xdot_vec,&nextTimePointIndex);
480  }
481 
482  }
483 
484  // Observe end of a time integration
485  if (!is_null(integrationObserver_)) {
486  integrationObserver_->observeEndTimeIntegration(*stepper_);
487  }
488 
489  if ( includesVerbLevel(verbLevel,Teuchos::VERB_LOW) )
490  *out << "\nLeaving " << this->Describable::description() << "::getFwdPoints(...) ...\n";
491 
492 }
493 
494 
495 template<class Scalar>
498 {
499  return timeRange(
500  stepper_->getTimeRange().lower(),
501  integrationTimeDomain_.upper()
502  );
503 }
504 
505 
506 // Overridden from InterpolationBufferBase
507 
508 
509 template<class Scalar>
510 RCP<const Thyra::VectorSpaceBase<Scalar> >
512 {
513  return stepper_->get_x_space();
514 }
515 
516 
517 template<class Scalar>
519  const Array<Scalar>& time_vec,
520  const Array<RCP<const Thyra::VectorBase<Scalar> > >& x_vec,
521  const Array<RCP<const Thyra::VectorBase<Scalar> > >& xdot_vec
522  )
523 {
524  stepper_->addPoints(time_vec,x_vec,xdot_vec);
525 }
526 
527 
528 template<class Scalar>
530  const Array<Scalar>& time_vec,
531  Array<RCP<const Thyra::VectorBase<Scalar> > >* x_vec,
532  Array<RCP<const Thyra::VectorBase<Scalar> > >* xdot_vec,
533  Array<ScalarMag>* accuracy_vec
534  ) const
535 {
536 // if (nonnull(trailingInterpBuffer_)) {
537 // int nextTimePointIndex = 0;
538 // getCurrentPoints(*trailingInterpBuffer_, time_vec, x_vec, xdot_vec, &nextTimePointIndex);
539 // getCurrentPoints(*stepper_, time_vec, x_vec, xdot_vec, &nextTimePointIndex);
540 // TEUCHOS_TEST_FOR_EXCEPTION( nextTimePointIndex < Teuchos::as<int>(time_vec.size()),
541 // std::out_of_range,
542 // "Error, the time point time_vec["<<nextTimePointIndex<<"] = "
543 // << time_vec[nextTimePointIndex] << " falls outside of the time range "
544 // << getTimeRange() << "!"
545 // );
546 // }
547 // else {
548  stepper_->getPoints(time_vec, x_vec, xdot_vec, accuracy_vec);
549 // }
550 }
551 
552 
553 template<class Scalar>
555 {
556  if (nonnull(trailingInterpBuffer_)) {
557  return timeRange(trailingInterpBuffer_->getTimeRange().lower(),
558  stepper_->getTimeRange().upper());
559  }
560  return stepper_->getTimeRange();
561 }
562 
563 
564 template<class Scalar>
565 void DefaultIntegrator<Scalar>::getNodes(Array<Scalar>* time_vec) const
566 {
567  stepper_->getNodes(time_vec);
568 }
569 
570 
571 template<class Scalar>
572 void DefaultIntegrator<Scalar>::removeNodes(Array<Scalar>& time_vec)
573 {
574  stepper_->removeNodes(time_vec);
575 }
576 
577 
578 template<class Scalar>
580 {
581  return stepper_->getOrder();
582 }
583 
584 
585 // private
586 
587 
588 template<class Scalar>
590 {
591  if (!is_null(trailingInterpBuffer_) && is_null(interpBufferAppender_))
592  interpBufferAppender_ = pointwiseInterpolationBufferAppender<Scalar>();
593  // ToDo: Do other setup?
594 }
595 
596 
597 template<class Scalar>
598 bool DefaultIntegrator<Scalar>::advanceStepperToTime( const Scalar& advance_to_t )
599 {
600 
601  RYTHMOS_FUNC_TIME_MONITOR_DIFF("Rythmos:DefaultIntegrator::advanceStepperToTime",
602  TopLevel);
603 
604  using std::endl;
605  typedef std::numeric_limits<Scalar> NL;
606  using Teuchos::incrVerbLevel;
607 #ifndef _MSC_VER
608  using Teuchos::Describable;
609 #endif
610  using Teuchos::OSTab;
611  typedef Teuchos::ScalarTraits<Scalar> ST;
612 
613  RCP<Teuchos::FancyOStream> out = this->getOStream();
614  Teuchos::EVerbosityLevel verbLevel = this->getVerbLevel();
615 
616  if (!is_null(integrationControlStrategy_)) {
617  integrationControlStrategy_->setOStream(out);
618  integrationControlStrategy_->setVerbLevel(incrVerbLevel(verbLevel,-1));
619  }
620 
621  if (!is_null(integrationObserver_)) {
622  integrationObserver_->setOStream(out);
623  integrationObserver_->setVerbLevel(incrVerbLevel(verbLevel,-1));
624  }
625 
626  if ( includesVerbLevel(verbLevel,Teuchos::VERB_LOW) )
627  *out << "\nEntering " << this->Describable::description()
628  << "::advanceStepperToTime("<<advance_to_t<<") ...\n";
629 
630  // Remember what timestep index we are on so we can report it later
631  const int initCurrTimeStepIndex = currTimeStepIndex_;
632 
633  // Take steps until we the requested time is reached (or passed)
634 
635  TimeRange<Scalar> currStepperTimeRange = stepper_->getTimeRange();
636 
637  // Start by assume we can reach the time advance_to_t
638  bool return_val = true;
639 
640  while ( !currStepperTimeRange.isInRange(advance_to_t) ) {
641 
642  // Halt immediatly if exceeded max iterations
643  if (currTimeStepIndex_ >= maxNumTimeSteps_) {
644  if ( includesVerbLevel(verbLevel,Teuchos::VERB_LOW) )
645  *out
646  << "\n***"
647  << "\n*** NOTICE: currTimeStepIndex = "<<currTimeStepIndex_
648  << " >= maxNumTimeSteps = "<<maxNumTimeSteps_<< ", halting time integration!"
649  << "\n***\n";
650  return_val = false;
651  break; // Exit the loop immediately!
652  }
653 
654  if ( includesVerbLevel(verbLevel,Teuchos::VERB_LOW) ) {
655  *out << "\nTake step: current_stepper_t = " << currStepperTimeRange.upper()
656  << ", currTimeStepIndex = " << currTimeStepIndex_ << endl;
657  }
658 
659  OSTab tab(out);
660 
661  //
662  // A) Reinitialize if a hard breakpoint was reached on the last time step
663  //
664 
665  if (stepCtrlInfoLast_.limitedByBreakPoint) {
666  if ( stepCtrlInfoLast_.breakPointType == BREAK_POINT_TYPE_HARD ) {
667  RYTHMOS_FUNC_TIME_MONITOR("Rythmos:DefaultIntegrator::restart");
668  if ( includesVerbLevel(verbLevel,Teuchos::VERB_LOW) )
669  *out << "\nAt a hard-breakpoint, restarting time integrator ...\n";
670  restart(&*stepper_);
671  }
672  else {
673  if ( includesVerbLevel(verbLevel,Teuchos::VERB_LOW) )
674  *out << "\nAt a soft-breakpoint, NOT restarting time integrator ...\n";
675  }
676  }
677 
678  //
679  // B) Find an acceptable time step in a loop
680  //
681  // NOTE: Look for continue statements to iterate the loop!
682  //
683 
684  bool foundAcceptableTimeStep = false;
685  StepControlInfo<Scalar> stepCtrlInfo;
686 
687  // \todo Limit the maximum number of trial time steps to avoid an infinite
688  // loop!
689 
690  while (!foundAcceptableTimeStep) {
691 
692  //
693  // B.1) Get the trial step control info
694  //
695 
696  StepControlInfo<Scalar> trialStepCtrlInfo;
697  {
698  RYTHMOS_FUNC_TIME_MONITOR("Rythmos:DefaultIntegrator::advanceStepperToTime: getStepCtrl");
699  if (!is_null(integrationControlStrategy_)) {
700  // Let an external strategy object determine the step size and type.
701  // Note that any breakpoint info is also related through this call.
702  trialStepCtrlInfo = integrationControlStrategy_->getNextStepControlInfo(
703  *stepper_, stepCtrlInfoLast_, currTimeStepIndex_
704  );
705  }
706  else {
707  // Take a variable step if we have no control strategy
708  trialStepCtrlInfo.stepType = STEP_TYPE_VARIABLE;
709  trialStepCtrlInfo.stepSize = NL::max();
710  }
711  }
712 
713  // Print the initial trial step
714  if ( includesVerbLevel(verbLevel,Teuchos::VERB_MEDIUM) ) {
715  *out << "\nTrial step:\n";
716  OSTab tab2(out);
717  *out << trialStepCtrlInfo;
718  }
719 
720  // Halt immediately if we where told to do so
721  if (trialStepCtrlInfo.stepSize < ST::zero()) {
722  if ( includesVerbLevel(verbLevel,Teuchos::VERB_MEDIUM) )
723  *out
724  << "\n***"
725  << "\n*** NOTICE: The IntegrationControlStrategy object return stepSize < 0.0, halting time integration!"
726  << "\n***\n";
727  return_val = false;
728  break; // Exit the loop immediately!
729  }
730 
731  // Make sure we don't step past the final time if asked not to
732  bool updatedTrialStepCtrlInfo = false;
733  {
734  const Scalar finalTime = integrationTimeDomain_.upper();
735  if (landOnFinalTime_ && trialStepCtrlInfo.stepSize + currStepperTimeRange.upper() > finalTime) {
736  if ( includesVerbLevel(verbLevel,Teuchos::VERB_LOW) )
737  *out << "\nCutting trial step to avoid stepping past final time ...\n";
738  trialStepCtrlInfo.stepSize = finalTime - currStepperTimeRange.upper();
739  updatedTrialStepCtrlInfo = true;
740  }
741  }
742 
743  // Print the modified trial step
744  if ( updatedTrialStepCtrlInfo
745  && includesVerbLevel(verbLevel,Teuchos::VERB_MEDIUM) )
746  {
747  *out << "\nUpdated trial step:\n";
748  OSTab tab2(out);
749  *out << trialStepCtrlInfo;
750  }
751 
752  //
753  // B.2) Take the step
754  //
755 
756  // Output to the observer we are starting a step
757  if (!is_null(integrationObserver_))
758  integrationObserver_->observeStartTimeStep(
759  *stepper_, trialStepCtrlInfo, currTimeStepIndex_
760  );
761 
762  // Print step type and size
763  if ( includesVerbLevel(verbLevel,Teuchos::VERB_MEDIUM) ) {
764  if (trialStepCtrlInfo.stepType == STEP_TYPE_VARIABLE)
765  *out << "\nTaking a variable time step with max step size = "
766  << trialStepCtrlInfo.stepSize << " ....\n";
767  else
768  *out << "\nTaking a fixed time step of size = "
769  << trialStepCtrlInfo.stepSize << " ....\n";
770  }
771 
772  // Take step
773  Scalar stepSizeTaken;
774  {
775  RYTHMOS_FUNC_TIME_MONITOR("Rythmos:DefaultIntegrator::advanceStepperToTime: takeStep");
776  stepSizeTaken = stepper_->takeStep(
777  trialStepCtrlInfo.stepSize, trialStepCtrlInfo.stepType
778  );
779  }
780 
781  // Update info about this step
782  currStepperTimeRange = stepper_->getTimeRange();
783  stepCtrlInfo = stepCtrlInfoTaken(trialStepCtrlInfo,stepSizeTaken);
784 
785  // Print the step actually taken
786  if ( includesVerbLevel(verbLevel,Teuchos::VERB_MEDIUM) ) {
787  *out << "\nStep actually taken:\n";
788  OSTab tab2(out);
789  *out << stepCtrlInfo;
790  }
791 
792  // Determine if the timestep failed
793  const bool timeStepFailed = (stepCtrlInfo.stepSize <= ST::zero());
794  if (timeStepFailed && includesVerbLevel(verbLevel,Teuchos::VERB_MEDIUM)) {
795  *out << "\nWARNING: timeStep = "<<trialStepCtrlInfo.stepSize<<" failed!\n";
796  }
797 
798  // Notify observer of a failed time step
799  if (timeStepFailed) {
800  if (nonnull(integrationObserver_))
801  integrationObserver_->observeFailedTimeStep(
802  *stepper_, stepCtrlInfo, currTimeStepIndex_
803  );
804 
805  // Allow the IntegrationControlStrategy object to suggest another timestep
806  const bool handlesFailedTimeSteps =
807  nonnull(integrationControlStrategy_) &&
808  integrationControlStrategy_->handlesFailedTimeSteps();
809  if (handlesFailedTimeSteps)
810  {
811  // See if a new timestep can be suggested
812  if (integrationControlStrategy_->resetForFailedTimeStep(
813  *stepper_, stepCtrlInfoLast_, currTimeStepIndex_, trialStepCtrlInfo)
814  )
815  {
816  if ( includesVerbLevel(verbLevel,Teuchos::VERB_MEDIUM) ) {
817  *out << "\nThe IntegrationControlStrategy object indicated that"
818  << " it would like to suggest another timestep!\n";
819  }
820  // Skip the rest of the code in the loop and back to the top to try
821  // another timestep! Note: By doing this we skip the statement that
822  // sets
823  continue;
824  }
825  else
826  {
827  if ( includesVerbLevel(verbLevel,Teuchos::VERB_MEDIUM) ) {
828  *out << "\nThe IntegrationControlStrategy object could not suggest"
829  << " a better time step! Allowing to fail the time step!\n";
830  }
831  // Fall through to the failure checking!
832  }
833  }
834  }
835 
836  // Validate step taken
837  if (trialStepCtrlInfo.stepType == STEP_TYPE_VARIABLE) {
838  TEUCHOS_TEST_FOR_EXCEPTION(
839  stepSizeTaken < ST::zero(), std::logic_error,
840  "Error, stepper took negative step of dt = " << stepSizeTaken << "!\n"
841  );
842  TEUCHOS_TEST_FOR_EXCEPTION(
843  stepSizeTaken > trialStepCtrlInfo.stepSize, std::logic_error,
844  "Error, stepper took step of dt = " << stepSizeTaken
845  << " > max step size of = " << trialStepCtrlInfo.stepSize << "!\n"
846  );
847  }
848  else { // STEP_TYPE_FIXED
849  TEUCHOS_TEST_FOR_EXCEPTION(
850  stepSizeTaken != trialStepCtrlInfo.stepSize, std::logic_error,
851  "Error, stepper took step of dt = " << stepSizeTaken
852  << " when asked to take step of dt = " << trialStepCtrlInfo.stepSize << "\n"
853  );
854  }
855 
856  // If we get here, the timestep is fine and is accepted!
857  foundAcceptableTimeStep = true;
858 
859  // Append the trailing interpolation buffer (if defined)
860  if (!is_null(trailingInterpBuffer_)) {
861  interpBufferAppender_->append(*stepper_,currStepperTimeRange,
862  trailingInterpBuffer_.ptr() );
863  }
864 
865  //
866  // B.3) Output info about step
867  //
868 
869  {
870 
871  RYTHMOS_FUNC_TIME_MONITOR("Rythmos:DefaultIntegrator::advanceStepperToTime: output");
872 
873  // Print our own brief output
874  if ( includesVerbLevel(verbLevel,Teuchos::VERB_MEDIUM) ) {
875  StepStatus<Scalar> stepStatus = stepper_->getStepStatus();
876  *out << "\nTime point reached = " << stepStatus.time << endl;
877  *out << "\nstepStatus:\n" << stepStatus;
878  if ( includesVerbLevel(verbLevel,Teuchos::VERB_EXTREME) ) {
879  RCP<const Thyra::VectorBase<Scalar> >
880  solution = stepStatus.solution,
881  solutionDot = stepStatus.solutionDot;
882  if (!is_null(solution))
883  *out << "\nsolution = \n" << Teuchos::describe(*solution,verbLevel);
884  if (!is_null(solutionDot))
885  *out << "\nsolutionDot = \n" << Teuchos::describe(*solutionDot,verbLevel);
886  }
887  }
888 
889  // Output to the observer
890  if (!is_null(integrationObserver_))
891  integrationObserver_->observeCompletedTimeStep(
892  *stepper_, stepCtrlInfo, currTimeStepIndex_
893  );
894 
895  }
896 
897  } // end loop to find a valid time step
898 
899  //
900  // C) Update info for next time step
901  //
902 
903  stepCtrlInfoLast_ = stepCtrlInfo;
904  ++currTimeStepIndex_;
905 
906  }
907 
908  if ( includesVerbLevel(verbLevel,Teuchos::VERB_LOW) )
909  *out << "\nNumber of steps taken in this call to advanceStepperToTime(...) = "
910  << (currTimeStepIndex_ - initCurrTimeStepIndex) << endl
911  << "\nLeaving" << this->Describable::description()
912  << "::advanceStepperToTime("<<advance_to_t<<") ...\n";
913 
914  return return_val;
915 
916 }
917 
918 //
919 // Explicit Instantiation macro
920 //
921 // Must be expanded from within the Rythmos namespace!
922 //
923 
924 #define RYTHMOS_DEFAULT_INTEGRATOR_INSTANT(SCALAR) \
925  \
926  template class DefaultIntegrator< SCALAR >; \
927  \
928  template RCP<DefaultIntegrator< SCALAR > > \
929  defaultIntegrator(); \
930  \
931  template RCP<DefaultIntegrator< SCALAR > > \
932  defaultIntegrator( \
933  const RCP<IntegrationControlStrategyBase< SCALAR > > &integrationControlStrategy, \
934  const RCP<IntegrationObserverBase< SCALAR > > &integrationObserver \
935  ); \
936  \
937  template RCP<DefaultIntegrator< SCALAR > > \
938  controlledDefaultIntegrator( \
939  const RCP<IntegrationControlStrategyBase< SCALAR > > &integrationControlStrategy \
940  ); \
941  \
942  template RCP<DefaultIntegrator< SCALAR > > \
943  observedDefaultIntegrator( \
944  const RCP<IntegrationObserverBase< SCALAR > > &integrationObserver \
945  );
946 
947 } // namespace Rythmos
948 
949 
950 #endif //RYTHMOS_DEFAULT_INTEGRATOR_DEF_HPP
RCP< const ParameterList > getValidParameters() const
RCP< const Thyra::VectorSpaceBase< Scalar > > get_x_space() const
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< StepperBase< Scalar > > unSetStepper()
RCP< const InterpolationBufferAppenderBase< Scalar > > getInterpolationBufferAppender()
RCP< InterpolationBufferBase< Scalar > > unSetTrailingInterpolationBuffer()
Base class for defining stepper functionality.
RCP< IntegratorBase< Scalar > > cloneIntegrator() const
RCP< const StepperBase< Scalar > > getStepper() const
RCP< IntegrationControlStrategyBase< Scalar > > getNonconstIntegrationControlStrategy()
Simple strict to aggregate integration/stepper control information.
void getNodes(Array< Scalar > *time_vec) const
Base class for strategy objects that control integration by selecting step sizes for a stepper...
RCP< const IntegrationControlStrategyBase< Scalar > > getIntegrationControlStrategy() const
TimeRange< Scalar > getFwdTimeRange() const
Base class for strategy objects that observe and time integration by observing the stepper object...
RCP< DefaultIntegrator< Scalar > > defaultIntegrator(const RCP< IntegrationControlStrategyBase< Scalar > > &integrationControlStrategy, const RCP< IntegrationObserverBase< Scalar > > &integrationObserver)
RCP< InterpolationBufferAppenderBase< Scalar > > unSetInterpolationBufferAppender()
RCP< DefaultIntegrator< Scalar > > defaultIntegrator()
void setIntegrationControlStrategy(const RCP< IntegrationControlStrategyBase< Scalar > > &integrationControlStrategy)
A concrete subclass for IntegratorBase that allows a good deal of customization.
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)
void setTrailingInterpolationBuffer(const RCP< InterpolationBufferBase< Scalar > > &trailingInterpBuffer)
RCP< StepperBase< Scalar > > getNonconstStepper() const
Base class for an interpolation buffer.
RCP< const InterpolationBufferBase< Scalar > > getTrailingInterpolationBuffer() const
RCP< DefaultIntegrator< Scalar > > controlledDefaultIntegrator(const RCP< IntegrationControlStrategyBase< Scalar > > &integrationControlStrategy)
void getFwdPoints(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)
Base class for strategy objects that append data from one InterplationBufferBase object to another...
void removeNodes(Array< Scalar > &time_vec)
void setInterpolationBufferAppender(const RCP< InterpolationBufferAppenderBase< Scalar > > &interpBufferAppender)
RCP< InterpolationBufferBase< Scalar > > getNonconstTrailingInterpolationBuffer()
TimeRange< Scalar > getTimeRange() const
RCP< DefaultIntegrator< Scalar > > observedDefaultIntegrator(const RCP< IntegrationObserverBase< Scalar > > &integrationObserver)
RCP< InterpolationBufferAppenderBase< Scalar > > getNonconstInterpolationBufferAppender()
void setIntegrationObserver(const RCP< IntegrationObserverBase< Scalar > > &integrationObserver)
void setParameterList(RCP< ParameterList > const &paramList)
void setStepper(const RCP< StepperBase< Scalar > > &stepper, const Scalar &finalTime, const bool landOnFinalTime=true)