9 #ifndef Tempus_StepperNewmarkImplicitAForm_impl_hpp 
   10 #define Tempus_StepperNewmarkImplicitAForm_impl_hpp 
   12 #include "Tempus_config.hpp" 
   14 #include "Teuchos_VerboseObjectParameterListHelpers.hpp" 
   15 #include "NOX_Thyra.H" 
   23 template<
class Scalar> 
class StepperFactory;
 
   26 template<
class Scalar>
 
   29                 const Thyra::VectorBase<Scalar>& v,
 
   30                 const Thyra::VectorBase<Scalar>& a,
 
   31                 const Scalar dt)
 const 
   33 #ifdef VERBOSE_DEBUG_OUTPUT 
   34   *out_ << 
"DEBUG: " << __PRETTY_FUNCTION__ << 
"\n";
 
   37   Thyra::V_StVpStV(Teuchos::ptrFromRef(vPred), 1.0, v, dt*(1.0-gamma_), a);
 
   40 template<
class Scalar>
 
   43                     const Thyra::VectorBase<Scalar>& d,
 
   44                     const Thyra::VectorBase<Scalar>& v,
 
   45                     const Thyra::VectorBase<Scalar>& a,
 
   46                     const Scalar dt)
 const 
   48 #ifdef VERBOSE_DEBUG_OUTPUT 
   49   *out_ << 
"DEBUG: " << __PRETTY_FUNCTION__ << 
"\n";
 
   51   Teuchos::RCP<const Thyra::VectorBase<Scalar> > tmp =
 
   52     Thyra::createMember<Scalar>(dPred.space());
 
   54   Scalar aConst = dt*dt/2.0*(1.0-2.0*beta_);
 
   55   Thyra::V_StVpStV(Teuchos::ptrFromRef(dPred), dt, v, aConst, a);
 
   57   Thyra::Vp_V(Teuchos::ptrFromRef(dPred), d, 1.0);
 
   60 template<
class Scalar>
 
   63                 const Thyra::VectorBase<Scalar>& vPred,
 
   64                 const Thyra::VectorBase<Scalar>& a,
 
   65                 const Scalar dt)
 const 
   67 #ifdef VERBOSE_DEBUG_OUTPUT 
   68   *out_ << 
"DEBUG: " << __PRETTY_FUNCTION__ << 
"\n";
 
   71   Thyra::V_StVpStV(Teuchos::ptrFromRef(v), 1.0, vPred, dt*gamma_, a);
 
   74 template<
class Scalar>
 
   77                     const Thyra::VectorBase<Scalar>& dPred,
 
   78                     const Thyra::VectorBase<Scalar>& a,
 
   79                     const Scalar dt)
 const 
   81 #ifdef VERBOSE_DEBUG_OUTPUT 
   82   *out_ << 
"DEBUG: " << __PRETTY_FUNCTION__ << 
"\n";
 
   85   Thyra::V_StVpStV(Teuchos::ptrFromRef(d), 1.0, dPred, beta_*dt*dt, a);
 
   89 template<
class Scalar>
 
   91   out_(Teuchos::VerboseObjectBase::getDefaultOStream())
 
   93 #ifdef VERBOSE_DEBUG_OUTPUT 
   94   *
out_ << 
"DEBUG: " << __PRETTY_FUNCTION__ << 
"\n";
 
  102 template<
class Scalar>
 
  104   const Teuchos::RCP<
const Thyra::ModelEvaluator<Scalar> >& appModel,
 
  105   Teuchos::RCP<Teuchos::ParameterList> pList) :
 
  106   out_(Teuchos::VerboseObjectBase::getDefaultOStream())
 
  108 #ifdef VERBOSE_DEBUG_OUTPUT 
  109   *
out_ << 
"DEBUG: " << __PRETTY_FUNCTION__ << 
"\n";
 
  114   if (appModel == Teuchos::null) {
 
  124 template<
class Scalar>
 
  126   const Teuchos::RCP<
const Thyra::ModelEvaluator<Scalar> >& appModel)
 
  128 #ifdef VERBOSE_DEBUG_OUTPUT 
  129   *out_ << 
"DEBUG: " << __PRETTY_FUNCTION__ << 
"\n";
 
  131   this->validSecondOrderODE_DAE(appModel);
 
  134                                               "Newmark Implicit a-Form"));
 
  135   this->wrapperModel_ = wrapperModel;
 
  139 template<
class Scalar>
 
  142   TEUCHOS_TEST_FOR_EXCEPTION( this->wrapperModel_ == Teuchos::null,
 
  144     "Error - Need to set the model, setModel(), before calling " 
  145     "StepperNewmarkImplicitAForm::initialize()\n");
 
  147 #ifdef VERBOSE_DEBUG_OUTPUT 
  148   *out_ << 
"DEBUG: " << __PRETTY_FUNCTION__ << 
"\n";
 
  150   this->setParameterList(this->stepperPL_);
 
  155 template<
class Scalar>
 
  163   TEUCHOS_TEST_FOR_EXCEPTION(numStates < 1, std::logic_error,
 
  164     "Error - setInitialConditions() needs at least one SolutionState\n" 
  165     "        to set the initial condition.  Number of States = " << numStates);
 
  168     RCP<Teuchos::FancyOStream> out = this->getOStream();
 
  169     Teuchos::OSTab ostab(out,1,
"StepperNewmarkImplicitAForm::setInitialConditions()");
 
  170     *out << 
"Warning -- SolutionHistory has more than one state!\n" 
  171          << 
"Setting the initial conditions on the currentState.\n"<<std::endl;
 
  174   RCP<SolutionState<Scalar> > initialState = 
solutionHistory->getCurrentState();
 
  175   RCP<Thyra::VectorBase<Scalar> > x    = initialState->getX();
 
  176   RCP<Thyra::VectorBase<Scalar> > xDot = initialState->getXDot();
 
  178   auto inArgs = this->wrapperModel_->getNominalValues();
 
  179   TEUCHOS_TEST_FOR_EXCEPTION(
 
  180     !((x != Teuchos::null && xDot != Teuchos::null) ||
 
  181       (inArgs.get_x() != Teuchos::null &&
 
  182        inArgs.get_x_dot() != Teuchos::null)), std::logic_error,
 
  183     "Error - We need to set the initial conditions for x and xDot from\n" 
  184     "        either initialState or appModel_->getNominalValues::InArgs\n" 
  185     "        (but not from a mixture of the two).\n");
 
  188   if ( x == Teuchos::null || xDot == Teuchos::null ) {
 
  189     using Teuchos::rcp_const_cast;
 
  190     TEUCHOS_TEST_FOR_EXCEPTION( (inArgs.get_x() == Teuchos::null) ||
 
  191       (inArgs.get_x_dot() == Teuchos::null), std::logic_error,
 
  192       "Error - setInitialConditions() needs the ICs from the initialState\n" 
  193       "        or getNominalValues()!\n");
 
  194     x    = rcp_const_cast<Thyra::VectorBase<Scalar> >(inArgs.get_x());
 
  195     initialState->setX(x);
 
  196     xDot = rcp_const_cast<Thyra::VectorBase<Scalar> >(inArgs.get_x_dot());
 
  197     initialState->setXDot(xDot);
 
  201   if (initialState->getXDotDot() == Teuchos::null)
 
  202     initialState->setXDotDot(initialState->getX()->clone_v());
 
  205   std::string icConsistency = this->getICConsistency();
 
  206   if (icConsistency == 
"None") {
 
  207     if (initialState->getXDotDot() == Teuchos::null) {
 
  208       RCP<Teuchos::FancyOStream> out = this->getOStream();
 
  209       Teuchos::OSTab ostab(out,1,
 
  210         "StepperNewmarkImplicitAForm::setInitialConditions()");
 
  211       *out << 
"Warning -- Requested IC consistency of 'None' but\n" 
  212            << 
"           initialState does not have an xDot.\n" 
  213            << 
"           Setting a 'Zero' xDot!\n" << std::endl;
 
  215       Thyra::assign(this->getStepperXDotDot(initialState).ptr(), Scalar(0.0));
 
  218   else if (icConsistency == 
"Zero")
 
  219     Thyra::assign(this->getStepperXDotDot(initialState).ptr(), Scalar(0.0));
 
  220   else if (icConsistency == 
"App") {
 
  221     auto xDotDot = Teuchos::rcp_const_cast<Thyra::VectorBase<Scalar> >(
 
  222                 inArgs.get_x_dot_dot());
 
  223     TEUCHOS_TEST_FOR_EXCEPTION(xDotDot == Teuchos::null, std::logic_error,
 
  224       "Error - setInitialConditions() requested 'App' for IC consistency,\n" 
  225       "        but 'App' returned a null pointer for xDotDot!\n");
 
  226     Thyra::assign(this->getStepperXDotDot(initialState).ptr(), *xDotDot);
 
  228   else if (icConsistency == 
"Consistent") {
 
  230     const Scalar time = initialState->getTime();
 
  231     auto xDotDot = this->getStepperXDotDot(initialState);
 
  235     if (this->initial_guess_ != Teuchos::null) {
 
  236       TEUCHOS_TEST_FOR_EXCEPTION(
 
  237         !((xDotDot->space())->isCompatible(*this->initial_guess_->space())),
 
  239         "Error - User-provided initial guess for Newton is not compatible\n" 
  240         "        with solution vector!\n");
 
  241       Thyra::copy(*this->initial_guess_, xDotDot.ptr());
 
  244       Thyra::put_scalar(0.0, xDotDot.ptr());
 
  249         this->wrapperModel_);
 
  252     const Thyra::SolveStatus<Scalar> sStatus = this->solveImplicitODE(xDotDot);
 
  254     TEUCHOS_TEST_FOR_EXCEPTION(
 
  255       sStatus.solveStatus != Thyra::SOLVE_STATUS_CONVERGED, std::logic_error,
 
  256       "Error - Solver failed while determining the initial conditions.\n" 
  260     TEUCHOS_TEST_FOR_EXCEPTION(
true, std::logic_error,
 
  261       "Error - setInitialConditions() invalid IC consistency, " 
  262       << icConsistency << 
".\n");
 
  267   initialState->setIsSynced(
true);
 
  270   if (this->getICConsistencyCheck()) {
 
  271     auto f       = initialState->getX()->clone_v();
 
  272     auto xDotDot = this->getStepperXDotDot(initialState);
 
  274     typedef Thyra::ModelEvaluatorBase MEB;
 
  275     MEB::InArgs<Scalar>  appInArgs =
 
  276       this->wrapperModel_->getAppModel()->createInArgs();
 
  277     MEB::OutArgs<Scalar> appOutArgs =
 
  278       this->wrapperModel_->getAppModel()->createOutArgs();
 
  280     appInArgs.set_x        (x      );
 
  281     appInArgs.set_x_dot    (xDot   );
 
  282     appInArgs.set_x_dot_dot(xDotDot);
 
  284     appOutArgs.set_f(appOutArgs.get_f());
 
  286     appInArgs.set_W_x_dot_dot_coeff(Scalar(0.0));     
 
  287     appInArgs.set_alpha            (Scalar(0.0));     
 
  288     appInArgs.set_beta             (Scalar(0.0));     
 
  290     appInArgs.set_t        (initialState->getTime()    );
 
  292     this->wrapperModel_->getAppModel()->evalModel(appInArgs, appOutArgs);
 
  294     Scalar reldiff = Thyra::norm(*f);
 
  295     Scalar normx = Thyra::norm(*x); 
 
  296     Scalar eps = Scalar(100.0)*std::abs(Teuchos::ScalarTraits<Scalar>::eps());
 
  297     if (normx > eps*reldiff) reldiff /= normx; 
 
  300       RCP<Teuchos::FancyOStream> out = this->getOStream();
 
  301       Teuchos::OSTab ostab(out,1,
 
  302         "StepperNewmarkImplicitAForm::setInitialConditions()");
 
  303       *out << 
"Warning -- Failed consistency check but continuing!\n" 
  304          << 
"  ||f(x,xDot,xDotDot,t)||/||x|| > eps" << std::endl
 
  305          << 
"  ||f(x,xDot,xDotDot,t)||       = " << Thyra::norm(*f)<< std::endl
 
  306          << 
"  ||x||                         = " << Thyra::norm(*x)<< std::endl
 
  307          << 
"  ||f(x,xDot,xDotDot,t)||/||x|| = " << reldiff        << std::endl
 
  308          << 
"                            eps = " << eps            << std::endl;
 
  312   if (!(this->getUseFSAL())) {
 
  313     RCP<Teuchos::FancyOStream> out = this->getOStream();
 
  314     Teuchos::OSTab ostab(out,1,
 
  315       "StepperNewmarkImplicitAForm::setInitialConditions()");
 
  316     *out << 
"\nWarning -- The First-Step-As-Last (FSAL) principle is " 
  317          << 
"part of the Newmark Implicit A-Form.  The default is to " 
  318          << 
"set useFSAL=true, and useFSAL=false will be ignored." << std::endl;
 
  323 template<
class Scalar>
 
  327 #ifdef VERBOSE_DEBUG_OUTPUT 
  328   *out_ << 
"DEBUG: " << __PRETTY_FUNCTION__ << 
"\n";
 
  332   TEMPUS_FUNC_TIME_MONITOR(
"Tempus::StepperNewmarkImplicitAForm::takeStep()");
 
  336       "Error - StepperNewmarkImplicitAForm<Scalar>::takeStep(...)\n" 
  337       "Need at least two SolutionStates for NewmarkImplicitAForm.\n" 
  339       "Try setting in \"Solution History\" \"Storage Type\" = \"Undo\"\n" 
  340       "  or \"Storage Type\" = \"Static\" and \"Storage Limit\" = \"2\"\n");
 
  342     RCP<SolutionState<Scalar> > workingState=
solutionHistory->getWorkingState();
 
  343     RCP<SolutionState<Scalar> > currentState=
solutionHistory->getCurrentState();
 
  347         this->wrapperModel_);
 
  350     RCP<const Thyra::VectorBase<Scalar> > d_old = currentState->getX();
 
  351     RCP<const Thyra::VectorBase<Scalar> > v_old = currentState->getXDot();
 
  352     RCP<      Thyra::VectorBase<Scalar> > a_old = currentState->getXDotDot();
 
  355     RCP<Thyra::VectorBase<Scalar> > d_new = workingState->getX();
 
  356     RCP<Thyra::VectorBase<Scalar> > v_new = workingState->getXDot();
 
  357     RCP<Thyra::VectorBase<Scalar> > a_new = workingState->getXDotDot();
 
  360     const Scalar time = currentState->getTime();
 
  361     const Scalar dt = workingState->getTimeStep();
 
  366     if (!(this->getUseFSAL()) && workingState->getNConsecutiveFailures() == 0) {
 
  368                                       Scalar(0.0), Scalar(0.0));
 
  369       const Thyra::SolveStatus<Scalar> sStatus = this->solveImplicitODE(a_old);
 
  371       workingState->setSolutionStatus(sStatus);  
 
  375     predictDisplacement(*d_new, *d_old, *v_old, *a_old, dt);
 
  376     predictVelocity(*v_new, *v_old, *a_old, dt);
 
  379     wrapperModel->initializeNewmark(v_new,d_new,dt,t,beta_,gamma_);
 
  382     const Thyra::SolveStatus<Scalar> sStatus = this->solveImplicitODE(a_new);
 
  385     correctVelocity(*v_new, *v_new, *a_new, dt);
 
  386     correctDisplacement(*d_new, *d_new, *a_new, dt);
 
  388     workingState->setSolutionStatus(sStatus);  
 
  389     workingState->setOrder(this->getOrder());
 
  402 template<
class Scalar>
 
  403 Teuchos::RCP<Tempus::StepperState<Scalar> >
 
  407 #ifdef VERBOSE_DEBUG_OUTPUT 
  408   *out_ << 
"DEBUG: " << __PRETTY_FUNCTION__ << 
"\n";
 
  410   Teuchos::RCP<Tempus::StepperState<Scalar> > stepperState =
 
  416 template<
class Scalar>
 
  419 #ifdef VERBOSE_DEBUG_OUTPUT 
  420   *out_ << 
"DEBUG: " << __PRETTY_FUNCTION__ << 
"\n";
 
  422   std::string name = 
"Newmark Implicit a-Form";
 
  427 template<
class Scalar>
 
  429    Teuchos::FancyOStream               &out,
 
  430    const Teuchos::EVerbosityLevel      )
 const 
  432 #ifdef VERBOSE_DEBUG_OUTPUT 
  433   *out_ << 
"DEBUG: " << __PRETTY_FUNCTION__ << 
"\n";
 
  435   out << description() << 
"::describe:" << std::endl
 
  436       << 
"wrapperModel = " << this->wrapperModel_->description() << std::endl;
 
  440 template <
class Scalar>
 
  442   Teuchos::RCP<Teuchos::ParameterList> 
const& pList)
 
  444 #ifdef VERBOSE_DEBUG_OUTPUT 
  445   *out_ << 
"DEBUG: " << __PRETTY_FUNCTION__ << 
"\n";
 
  447   if (pList == Teuchos::null) {
 
  449     if (this->stepperPL_ == Teuchos::null) this->stepperPL_ = this->getDefaultParameters();
 
  451     this->stepperPL_ = pList;
 
  458   Teuchos::RCP<Teuchos::ParameterList> stepperPL = this->stepperPL_;
 
  459   std::string stepperType = stepperPL->get<std::string>(
"Stepper Type");
 
  460   TEUCHOS_TEST_FOR_EXCEPTION( stepperType != 
"Newmark Implicit a-Form",
 
  462        "Error - Stepper Type is not 'Newmark Implicit a-Form'!\n" 
  463        << 
"  Stepper Type = "<< stepperPL->get<std::string>(
"Stepper Type")
 
  467     Teuchos::VerboseObjectBase::getDefaultOStream();
 
  468   if (this->stepperPL_->isSublist(
"Newmark Parameters")) {
 
  469     Teuchos::ParameterList &newmarkPL =
 
  470       this->stepperPL_->sublist(
"Newmark Parameters", 
true);
 
  471     std::string scheme_name = newmarkPL.get(
"Scheme Name", 
"Not Specified");
 
  472     if (scheme_name == 
"Not Specified") {
 
  473       beta_ = newmarkPL.get(
"Beta", 0.25);
 
  474       gamma_ = newmarkPL.get(
"Gamma", 0.5);
 
  475       TEUCHOS_TEST_FOR_EXCEPTION( (beta_ > 1.0) || (beta_ < 0.0),
 
  477         "\nError in 'Newmark Implicit a-Form' stepper: invalid value of Beta = " 
  478         << beta_ << 
".  Please select Beta >= 0 and <= 1. \n");
 
  479       TEUCHOS_TEST_FOR_EXCEPTION( (gamma_ > 1.0) || (gamma_ < 0.0),
 
  481         "\nError in 'Newmark Implicit a-Form' stepper: invalid value of Gamma =" 
  482         <<gamma_ << 
".  Please select Gamma >= 0 and <= 1. \n");
 
  483       *out_ << 
"\nSetting Beta = " << beta_ << 
" and Gamma = " << gamma_
 
  484             << 
" from Newmark Parameters in input file.\n";
 
  487       *out_ << 
"\nScheme Name = " << scheme_name << 
".  Using values \n" 
  488             << 
"of Beta and Gamma for this scheme (ignoring values of " 
  489             << 
"Beta and Gamma \n" 
  490             << 
"in input file, if provided).\n";
 
  491        if (scheme_name == 
"Average Acceleration") {
 
  492          beta_ = 0.25; gamma_ = 0.5;
 
  494        else if (scheme_name == 
"Linear Acceleration") {
 
  495          beta_ = 0.25; gamma_ = 1.0/6.0;
 
  497        else if (scheme_name == 
"Central Difference") {
 
  498          beta_ = 0.0; gamma_ = 0.5;
 
  501          TEUCHOS_TEST_FOR_EXCEPTION(
true,
 
  503             "\nError in Tempus::StepperNewmarkImplicitAForm!  " 
  504             <<
"Invalid Scheme Name = " << scheme_name <<
".  \n" 
  505             <<
"Valid Scheme Names are: 'Average Acceleration', " 
  506             <<
"'Linear Acceleration', \n" 
  507             <<
"'Central Difference' and 'Not Specified'.\n");
 
  509        *out_ << 
"===> Beta = " << beta_ << 
", Gamma = " << gamma_ << 
"\n";
 
  512       *out_ << 
"\nWARNING: Running (implicit implementation of) Newmark " 
  513             << 
"Implicit a-Form Stepper with Beta = 0.0, which \n" 
  514             << 
"specifies an explicit scheme.  Mass lumping is not possible, " 
  515             << 
"so this will be slow!  To run explicit \n" 
  516             << 
"implementation of Newmark Implicit a-Form Stepper, please " 
  517             << 
"re-run with 'Stepper Type' = 'Newmark Explicit a-Form'.\n" 
  518             << 
"This stepper allows for mass lumping when called through " 
  519             << 
"Piro::TempusSolver.\n";
 
  523     *out_ << 
"\nNo Newmark Parameters sublist found in input file; using " 
  524           << 
"default values of Beta = " 
  525           << beta_ << 
" and Gamma = " << gamma_ << 
".\n";
 
  530 template<
class Scalar>
 
  531 Teuchos::RCP<const Teuchos::ParameterList>
 
  534 #ifdef VERBOSE_DEBUG_OUTPUT 
  535   *out_ << 
"DEBUG: " << __PRETTY_FUNCTION__ << 
"\n";
 
  537   Teuchos::RCP<Teuchos::ParameterList> pl = Teuchos::parameterList();
 
  538   pl->setName(
"Default Stepper - " + this->description());
 
  539   pl->set<std::string>(
"Stepper Type", this->description());
 
  540   this->getValidParametersBasic(pl);
 
  541   pl->set<
bool>       (
"Use FSAL", 
true);
 
  542   pl->set<std::string>(
"Initial Condition Consistency", 
"Consistent");
 
  543   pl->set<
bool>       (
"Zero Initial Guess", 
false);
 
  544   pl->set<std::string>(
"Solver Name", 
"",
 
  545     "Name of ParameterList containing the solver specifications.");
 
  549 template<
class Scalar>
 
  550 Teuchos::RCP<Teuchos::ParameterList>
 
  553 #ifdef VERBOSE_DEBUG_OUTPUT 
  554   *out_ << 
"DEBUG: " << __PRETTY_FUNCTION__ << 
"\n";
 
  557   using Teuchos::ParameterList;
 
  558   using Teuchos::rcp_const_cast;
 
  560   RCP<ParameterList> pl =
 
  561     rcp_const_cast<ParameterList>(this->getValidParameters());
 
  563   pl->set<std::string>(
"Solver Name", 
"Default Solver");
 
  564   RCP<ParameterList> solverPL = this->defaultSolverParameters();
 
  565   pl->set(
"Default Solver", *solverPL);
 
  571 template <
class Scalar>
 
  572 Teuchos::RCP<Teuchos::ParameterList>
 
  575 #ifdef VERBOSE_DEBUG_OUTPUT 
  576   *out_ << 
"DEBUG: " << __PRETTY_FUNCTION__ << 
"\n";
 
  578   return(this->stepperPL_);
 
  582 template <
class Scalar>
 
  583 Teuchos::RCP<Teuchos::ParameterList>
 
  586 #ifdef VERBOSE_DEBUG_OUTPUT 
  587   *out_ << 
"DEBUG: " << __PRETTY_FUNCTION__ << 
"\n";
 
  589   Teuchos::RCP<Teuchos::ParameterList> temp_plist = this->stepperPL_;
 
  590   this->stepperPL_ = Teuchos::null;
 
  596 #endif // Tempus_StepperNewmarkImplicitAForm_impl_hpp 
const std::string toString(const Status status)
Convert Status to string. 
 
void initializeNewmark(Teuchos::RCP< const Vector > v_pred, Teuchos::RCP< const Vector > d_pred, Scalar delta_t, Scalar t, Scalar beta, Scalar gamma)
Set values needed in evalModelImpl. 
 
virtual void modelWarning() const 
 
A ModelEvaluator for residual evaluations given a state. This ModelEvaluator takes a state...
 
StepperState is a simple class to hold state information about the stepper. 
 
Teuchos::RCP< SolutionHistory< Scalar > > solutionHistory(Teuchos::RCP< Teuchos::ParameterList > pList=Teuchos::null)
Nonmember constructor. 
 
SolutionHistory is basically a container of SolutionStates. SolutionHistory maintains a collection of...