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>
92 if (schemeName_ !=
"User Defined") {
93 *out_ <<
"\nWARNING: schemeName != 'User Defined' (=" <<schemeName_<<
").\n"
94 <<
" Not setting beta, and leaving as beta = " << beta_ <<
"!\n";
101 *out_ <<
"\nWARNING: Running (implicit implementation of) Newmark "
102 <<
"Implicit a-Form Stepper with Beta = 0.0, which \n"
103 <<
"specifies an explicit scheme. Mass lumping is not possible, "
104 <<
"so this will be slow! To run explicit \n"
105 <<
"implementation of Newmark Implicit a-Form Stepper, please "
106 <<
"re-run with 'Stepper Type' = 'Newmark Explicit a-Form'.\n"
107 <<
"This stepper allows for mass lumping when called through "
108 <<
"Piro::TempusSolver.\n";
111 TEUCHOS_TEST_FOR_EXCEPTION( (beta_ > 1.0) || (beta_ < 0.0),
113 "\nError in 'Newmark Implicit a-Form' stepper: invalid value of Beta = "
114 << beta_ <<
". Please select Beta >= 0 and <= 1. \n");
118 template<
class Scalar>
121 if (schemeName_ !=
"User Defined") {
122 *out_ <<
"\nWARNING: schemeName != 'User Defined' (=" <<schemeName_<<
").\n"
123 <<
" Not setting gamma, and leaving as gamma = " << gamma_ <<
"!\n";
129 TEUCHOS_TEST_FOR_EXCEPTION( (gamma_ > 1.0) || (gamma_ < 0.0),
131 "\nError in 'Newmark Implicit a-Form' stepper: invalid value of Gamma ="
132 <<gamma_ <<
". Please select Gamma >= 0 and <= 1. \n");
136 template<
class Scalar>
138 std::string schemeName)
140 schemeName_ = schemeName;
142 if (schemeName_ ==
"Average Acceleration") {
143 beta_= 0.25; gamma_ = 0.5;
145 else if (schemeName_ ==
"Linear Acceleration") {
146 beta_= 0.25; gamma_ = 1.0/6.0;
148 else if (schemeName_ ==
"Central Difference") {
149 beta_= 0.0; gamma_ = 0.5;
151 else if (schemeName_ ==
"User Defined") {
152 beta_= 0.25; gamma_ = 0.5;
155 TEUCHOS_TEST_FOR_EXCEPTION(
true,
157 "\nError in Tempus::StepperNewmarkImplicitAForm! "
158 <<
"Invalid Scheme Name = " << schemeName_ <<
". \n"
159 <<
"Valid Scheme Names are: 'Average Acceleration', "
160 <<
"'Linear Acceleration', \n"
161 <<
"'Central Difference' and 'User Defined'.\n");
166 template<
class Scalar>
168 out_(Teuchos::VerboseObjectBase::getDefaultOStream())
170 #ifdef VERBOSE_DEBUG_OUTPUT
171 *
out_ <<
"DEBUG: " << __PRETTY_FUNCTION__ <<
"\n";
185 template<
class Scalar>
187 const Teuchos::RCP<
const Thyra::ModelEvaluator<Scalar> >& appModel,
189 const Teuchos::RCP<Thyra::NonlinearSolverBase<Scalar> >& solver,
191 std::string ICConsistency,
192 bool ICConsistencyCheck,
193 bool zeroInitialGuess,
194 std::string schemeName,
197 : out_(Teuchos::VerboseObjectBase::getDefaultOStream())
210 if (appModel != Teuchos::null) {
219 template<
class Scalar>
221 const Teuchos::RCP<
const Thyra::ModelEvaluator<Scalar> >& appModel)
223 #ifdef VERBOSE_DEBUG_OUTPUT
224 *out_ <<
"DEBUG: " << __PRETTY_FUNCTION__ <<
"\n";
229 "Newmark Implicit a-Form"));
230 this->wrapperModel_ = wrapperModel;
234 template<
class Scalar>
237 TEUCHOS_TEST_FOR_EXCEPTION( this->wrapperModel_ == Teuchos::null,
239 "Error - Need to set the model, setModel(), before calling "
240 "StepperNewmarkImplicitAForm::initialize()\n");
242 #ifdef VERBOSE_DEBUG_OUTPUT
243 *out_ <<
"DEBUG: " << __PRETTY_FUNCTION__ <<
"\n";
248 template<
class Scalar>
256 TEUCHOS_TEST_FOR_EXCEPTION(numStates < 1, std::logic_error,
257 "Error - setInitialConditions() needs at least one SolutionState\n"
258 " to set the initial condition. Number of States = " << numStates);
261 RCP<Teuchos::FancyOStream> out = this->getOStream();
262 Teuchos::OSTab ostab(out,1,
"StepperNewmarkImplicitAForm::setInitialConditions()");
263 *out <<
"Warning -- SolutionHistory has more than one state!\n"
264 <<
"Setting the initial conditions on the currentState.\n"<<std::endl;
267 RCP<SolutionState<Scalar> > initialState =
solutionHistory->getCurrentState();
268 RCP<Thyra::VectorBase<Scalar> > x = initialState->getX();
269 RCP<Thyra::VectorBase<Scalar> > xDot = initialState->getXDot();
271 auto inArgs = this->wrapperModel_->getNominalValues();
272 TEUCHOS_TEST_FOR_EXCEPTION(
273 !((x != Teuchos::null && xDot != Teuchos::null) ||
274 (inArgs.get_x() != Teuchos::null &&
275 inArgs.get_x_dot() != Teuchos::null)), std::logic_error,
276 "Error - We need to set the initial conditions for x and xDot from\n"
277 " either initialState or appModel_->getNominalValues::InArgs\n"
278 " (but not from a mixture of the two).\n");
281 if ( x == Teuchos::null || xDot == Teuchos::null ) {
282 using Teuchos::rcp_const_cast;
283 TEUCHOS_TEST_FOR_EXCEPTION( (inArgs.get_x() == Teuchos::null) ||
284 (inArgs.get_x_dot() == Teuchos::null), std::logic_error,
285 "Error - setInitialConditions() needs the ICs from the initialState\n"
286 " or getNominalValues()!\n");
287 x = rcp_const_cast<Thyra::VectorBase<Scalar> >(inArgs.get_x());
288 initialState->setX(x);
289 xDot = rcp_const_cast<Thyra::VectorBase<Scalar> >(inArgs.get_x_dot());
290 initialState->setXDot(xDot);
294 if (initialState->getXDotDot() == Teuchos::null)
295 initialState->setXDotDot(initialState->getX()->clone_v());
298 std::string icConsistency = this->getICConsistency();
299 if (icConsistency ==
"None") {
300 if (initialState->getXDotDot() == Teuchos::null) {
301 RCP<Teuchos::FancyOStream> out = this->getOStream();
302 Teuchos::OSTab ostab(out,1,
303 "StepperNewmarkImplicitAForm::setInitialConditions()");
304 *out <<
"Warning -- Requested IC consistency of 'None' but\n"
305 <<
" initialState does not have an xDot.\n"
306 <<
" Setting a 'Zero' xDot!\n" << std::endl;
308 Thyra::assign(this->getStepperXDotDot(initialState).ptr(), Scalar(0.0));
311 else if (icConsistency ==
"Zero")
312 Thyra::assign(this->getStepperXDotDot(initialState).ptr(), Scalar(0.0));
313 else if (icConsistency ==
"App") {
314 auto xDotDot = Teuchos::rcp_const_cast<Thyra::VectorBase<Scalar> >(
315 inArgs.get_x_dot_dot());
316 TEUCHOS_TEST_FOR_EXCEPTION(xDotDot == Teuchos::null, std::logic_error,
317 "Error - setInitialConditions() requested 'App' for IC consistency,\n"
318 " but 'App' returned a null pointer for xDotDot!\n");
319 Thyra::assign(this->getStepperXDotDot(initialState).ptr(), *xDotDot);
321 else if (icConsistency ==
"Consistent") {
323 const Scalar time = initialState->getTime();
324 auto xDotDot = this->getStepperXDotDot(initialState);
328 if (this->initial_guess_ != Teuchos::null) {
329 TEUCHOS_TEST_FOR_EXCEPTION(
330 !((xDotDot->space())->isCompatible(*this->initial_guess_->space())),
332 "Error - User-provided initial guess for Newton is not compatible\n"
333 " with solution vector!\n");
334 Thyra::copy(*this->initial_guess_, xDotDot.ptr());
337 Thyra::put_scalar(0.0, xDotDot.ptr());
342 this->wrapperModel_);
345 const Thyra::SolveStatus<Scalar> sStatus = this->solveImplicitODE(xDotDot);
347 TEUCHOS_TEST_FOR_EXCEPTION(
348 sStatus.solveStatus != Thyra::SOLVE_STATUS_CONVERGED, std::logic_error,
349 "Error - Solver failed while determining the initial conditions.\n"
353 TEUCHOS_TEST_FOR_EXCEPTION(
true, std::logic_error,
354 "Error - setInitialConditions() invalid IC consistency, "
355 << icConsistency <<
".\n");
360 initialState->setIsSynced(
true);
363 if (this->getICConsistencyCheck()) {
364 auto f = initialState->getX()->clone_v();
365 auto xDotDot = this->getStepperXDotDot(initialState);
367 typedef Thyra::ModelEvaluatorBase MEB;
368 MEB::InArgs<Scalar> appInArgs =
369 this->wrapperModel_->getAppModel()->createInArgs();
370 MEB::OutArgs<Scalar> appOutArgs =
371 this->wrapperModel_->getAppModel()->createOutArgs();
373 appInArgs.set_x (x );
374 appInArgs.set_x_dot (xDot );
375 appInArgs.set_x_dot_dot(xDotDot);
377 appOutArgs.set_f(appOutArgs.get_f());
379 appInArgs.set_W_x_dot_dot_coeff(Scalar(0.0));
380 appInArgs.set_alpha (Scalar(0.0));
381 appInArgs.set_beta (Scalar(0.0));
383 appInArgs.set_t (initialState->getTime() );
385 this->wrapperModel_->getAppModel()->evalModel(appInArgs, appOutArgs);
387 Scalar reldiff = Thyra::norm(*f);
388 Scalar normx = Thyra::norm(*x);
389 Scalar eps = Scalar(100.0)*std::abs(Teuchos::ScalarTraits<Scalar>::eps());
390 if (normx > eps*reldiff) reldiff /= normx;
393 RCP<Teuchos::FancyOStream> out = this->getOStream();
394 Teuchos::OSTab ostab(out,1,
395 "StepperNewmarkImplicitAForm::setInitialConditions()");
396 *out <<
"Warning -- Failed consistency check but continuing!\n"
397 <<
" ||f(x,xDot,xDotDot,t)||/||x|| > eps" << std::endl
398 <<
" ||f(x,xDot,xDotDot,t)|| = " << Thyra::norm(*f)<< std::endl
399 <<
" ||x|| = " << Thyra::norm(*x)<< std::endl
400 <<
" ||f(x,xDot,xDotDot,t)||/||x|| = " << reldiff << std::endl
401 <<
" eps = " << eps << std::endl;
405 if (!(this->getUseFSAL())) {
406 RCP<Teuchos::FancyOStream> out = this->getOStream();
407 Teuchos::OSTab ostab(out,1,
408 "StepperNewmarkImplicitAForm::setInitialConditions()");
409 *out <<
"\nWarning -- The First-Step-As-Last (FSAL) principle is "
410 <<
"part of the Newmark Implicit A-Form. The default is to "
411 <<
"set useFSAL=true, and useFSAL=false will be ignored." << std::endl;
416 template<
class Scalar>
420 #ifdef VERBOSE_DEBUG_OUTPUT
421 *out_ <<
"DEBUG: " << __PRETTY_FUNCTION__ <<
"\n";
425 TEMPUS_FUNC_TIME_MONITOR(
"Tempus::StepperNewmarkImplicitAForm::takeStep()");
429 "Error - StepperNewmarkImplicitAForm<Scalar>::takeStep(...)\n"
430 "Need at least two SolutionStates for NewmarkImplicitAForm.\n"
432 "Try setting in \"Solution History\" \"Storage Type\" = \"Undo\"\n"
433 " or \"Storage Type\" = \"Static\" and \"Storage Limit\" = \"2\"\n");
435 RCP<SolutionState<Scalar> > workingState=
solutionHistory->getWorkingState();
436 RCP<SolutionState<Scalar> > currentState=
solutionHistory->getCurrentState();
440 this->wrapperModel_);
443 RCP<const Thyra::VectorBase<Scalar> > d_old = currentState->getX();
444 RCP<const Thyra::VectorBase<Scalar> > v_old = currentState->getXDot();
445 RCP< Thyra::VectorBase<Scalar> > a_old = currentState->getXDotDot();
448 RCP<Thyra::VectorBase<Scalar> > d_new = workingState->getX();
449 RCP<Thyra::VectorBase<Scalar> > v_new = workingState->getXDot();
450 RCP<Thyra::VectorBase<Scalar> > a_new = workingState->getXDotDot();
453 const Scalar time = currentState->getTime();
454 const Scalar dt = workingState->getTimeStep();
459 if (!(this->getUseFSAL()) && workingState->getNConsecutiveFailures() == 0) {
461 Scalar(0.0), Scalar(0.0));
462 const Thyra::SolveStatus<Scalar> sStatus = this->solveImplicitODE(a_old);
464 workingState->setSolutionStatus(sStatus);
468 predictDisplacement(*d_new, *d_old, *v_old, *a_old, dt);
469 predictVelocity(*v_new, *v_old, *a_old, dt);
472 wrapperModel->initializeNewmark(v_new,d_new,dt,t,beta_,gamma_);
475 const Thyra::SolveStatus<Scalar> sStatus = this->solveImplicitODE(a_new);
478 correctVelocity(*v_new, *v_new, *a_new, dt);
479 correctDisplacement(*d_new, *d_new, *a_new, dt);
481 workingState->setSolutionStatus(sStatus);
482 workingState->setOrder(this->getOrder());
495 template<
class Scalar>
496 Teuchos::RCP<Tempus::StepperState<Scalar> >
500 #ifdef VERBOSE_DEBUG_OUTPUT
501 *out_ <<
"DEBUG: " << __PRETTY_FUNCTION__ <<
"\n";
503 Teuchos::RCP<Tempus::StepperState<Scalar> > stepperState =
509 template<
class Scalar>
511 Teuchos::FancyOStream &out,
512 const Teuchos::EVerbosityLevel )
const
514 #ifdef VERBOSE_DEBUG_OUTPUT
515 *out_ <<
"DEBUG: " << __PRETTY_FUNCTION__ <<
"\n";
517 out << this->getStepperType() <<
"::describe:" << std::endl
518 <<
"wrapperModel = " << this->wrapperModel_->description() << std::endl;
522 template<
class Scalar>
523 Teuchos::RCP<const Teuchos::ParameterList>
526 #ifdef VERBOSE_DEBUG_OUTPUT
527 *out_ <<
"DEBUG: " << __PRETTY_FUNCTION__ <<
"\n";
529 Teuchos::RCP<Teuchos::ParameterList> pl = Teuchos::parameterList();
531 pl->set<std::string>(
"Scheme Name",
"Average Acceleration");
532 pl->set<
double> (
"Beta" , 0.25);
533 pl->set<
double> (
"Gamma", 0.5 );
534 pl->set<
bool> (
"Use FSAL", this->getUseFSALDefault());
535 pl->set<std::string>(
"Initial Condition Consistency",
536 this->getICConsistencyDefault());
537 pl->set<std::string>(
"Solver Name",
"Default Solver");
538 pl->set<
bool> (
"Zero Initial Guess",
false);
540 pl->set(
"Default Solver", *solverPL);
547 #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 setSolver(Teuchos::RCP< Thyra::NonlinearSolverBase< Scalar > > solver=Teuchos::null)
Set solver.
virtual bool getICConsistencyCheckDefault() const
Teuchos::RCP< Teuchos::ParameterList > defaultSolverParameters()
Returns the default solver ParameterList for implicit Steppers.
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.
void setICConsistencyCheck(bool c)
StepperObserver class for Stepper class.
Teuchos::RCP< SolutionHistory< Scalar > > solutionHistory(Teuchos::RCP< Teuchos::ParameterList > pList=Teuchos::null)
Nonmember constructor.
virtual void setZeroInitialGuess(bool zIG)
Set parameter so that the initial guess is set to zero (=True) or use last timestep (=False)...
SolutionHistory is basically a container of SolutionStates. SolutionHistory maintains a collection of...
void validSecondOrderODE_DAE(const Teuchos::RCP< const Thyra::ModelEvaluator< Scalar > > &model)
Validate ME supports 2nd order implicit ODE/DAE evaluation, f(xdotdot,xdot,x,t) [= 0]...
void getValidParametersBasic(Teuchos::RCP< Teuchos::ParameterList > pl, std::string stepperType)
Provide basic parameters to Steppers.
void setStepperType(std::string s)
void setICConsistency(std::string s)