10 #ifndef Tempus_StepperNewmarkImplicitAForm_impl_hpp
11 #define Tempus_StepperNewmarkImplicitAForm_impl_hpp
20 template <
class Scalar>
25 #ifdef VERBOSE_DEBUG_OUTPUT
26 *out_ <<
"DEBUG: " << __PRETTY_FUNCTION__ <<
"\n";
29 Thyra::V_StVpStV(Teuchos::ptrFromRef(vPred), 1.0, v, dt * (1.0 - gamma_), a);
32 template <
class Scalar>
36 const Scalar dt)
const
38 #ifdef VERBOSE_DEBUG_OUTPUT
39 *out_ <<
"DEBUG: " << __PRETTY_FUNCTION__ <<
"\n";
42 Thyra::createMember<Scalar>(dPred.
space());
44 Scalar aConst = dt * dt / 2.0 * (1.0 - 2.0 * beta_);
45 Thyra::V_StVpStV(Teuchos::ptrFromRef(dPred), dt, v, aConst, a);
47 Thyra::Vp_V(Teuchos::ptrFromRef(dPred), d, 1.0);
50 template <
class Scalar>
55 #ifdef VERBOSE_DEBUG_OUTPUT
56 *out_ <<
"DEBUG: " << __PRETTY_FUNCTION__ <<
"\n";
59 Thyra::V_StVpStV(Teuchos::ptrFromRef(v), 1.0, vPred, dt * gamma_, a);
62 template <
class Scalar>
67 #ifdef VERBOSE_DEBUG_OUTPUT
68 *out_ <<
"DEBUG: " << __PRETTY_FUNCTION__ <<
"\n";
71 Thyra::V_StVpStV(Teuchos::ptrFromRef(d), 1.0, dPred, beta_ * dt * dt, a);
74 template <
class Scalar>
77 if (schemeName_ !=
"User Defined") {
78 *out_ <<
"\nWARNING: schemeName != 'User Defined' (=" << schemeName_
80 <<
" Not setting beta, and leaving as beta = " << beta_ <<
"!\n";
87 *out_ <<
"\nWARNING: Running (implicit implementation of) Newmark "
88 <<
"Implicit a-Form Stepper with Beta = 0.0, which \n"
89 <<
"specifies an explicit scheme. Mass lumping is not possible, "
90 <<
"so this will be slow! To run explicit \n"
91 <<
"implementation of Newmark Implicit a-Form Stepper, please "
92 <<
"re-run with 'Stepper Type' = 'Newmark Explicit a-Form'.\n"
93 <<
"This stepper allows for mass lumping when called through "
94 <<
"Piro::TempusSolver.\n";
98 (beta_ > 1.0) || (beta_ < 0.0), std::logic_error,
99 "\nError in 'Newmark Implicit a-Form' stepper: invalid value of Beta = "
100 << beta_ <<
". Please select Beta >= 0 and <= 1. \n");
103 template <
class Scalar>
106 if (schemeName_ !=
"User Defined") {
107 *out_ <<
"\nWARNING: schemeName != 'User Defined' (=" << schemeName_
109 <<
" Not setting gamma, and leaving as gamma = " << gamma_ <<
"!\n";
116 (gamma_ > 1.0) || (gamma_ < 0.0), std::logic_error,
117 "\nError in 'Newmark Implicit a-Form' stepper: invalid value of Gamma ="
118 << gamma_ <<
". Please select Gamma >= 0 and <= 1. \n");
121 template <
class Scalar>
124 schemeName_ = schemeName;
126 if (schemeName_ ==
"Average Acceleration") {
130 else if (schemeName_ ==
"Linear Acceleration") {
134 else if (schemeName_ ==
"Central Difference") {
138 else if (schemeName_ ==
"User Defined") {
144 true, std::logic_error,
145 "\nError in Tempus::StepperNewmarkImplicitAForm! "
146 <<
"Invalid Scheme Name = " << schemeName_ <<
". \n"
147 <<
"Valid Scheme Names are: 'Average Acceleration', "
148 <<
"'Linear Acceleration', \n"
149 <<
"'Central Difference' and 'User Defined'.\n");
152 this->isInitialized_ =
false;
155 template <
class Scalar>
159 if (appAction == Teuchos::null) {
161 stepperNewmarkImpAppAction_ =
165 stepperNewmarkImpAppAction_ = appAction;
168 this->isInitialized_ =
false;
171 template <
class Scalar>
173 : out_(Teuchos::VerboseObjectBase::getDefaultOStream())
175 #ifdef VERBOSE_DEBUG_OUTPUT
176 *
out_ <<
"DEBUG: " << __PRETTY_FUNCTION__ <<
"\n";
191 template <
class Scalar>
195 bool useFSAL, std::string ICConsistency,
bool ICConsistencyCheck,
196 bool zeroInitialGuess, std::string schemeName, Scalar beta, Scalar gamma,
199 : out_(Teuchos::VerboseObjectBase::getDefaultOStream())
213 if (appModel != Teuchos::null) {
219 template <
class Scalar>
223 #ifdef VERBOSE_DEBUG_OUTPUT
224 *out_ <<
"DEBUG: " << __PRETTY_FUNCTION__ <<
"\n";
227 this->wrapperModel_ =
229 appModel,
"Newmark Implicit a-Form"));
232 std::logic_error,
"Error - Solver is not set!\n");
233 this->getSolver()->setModel(this->wrapperModel_);
235 this->isInitialized_ =
false;
238 template <
class Scalar>
244 int numStates = solutionHistory->getNumStates();
247 numStates < 1, std::logic_error,
248 "Error - setInitialConditions() needs at least one SolutionState\n"
249 " to set the initial condition. Number of States = "
253 RCP<Teuchos::FancyOStream> out = this->getOStream();
254 out->setOutputToRootOnly(0);
256 "StepperNewmarkImplicitAForm::setInitialConditions()");
257 *out <<
"Warning -- SolutionHistory has more than one state!\n"
258 <<
"Setting the initial conditions on the currentState.\n"
262 RCP<SolutionState<Scalar> > initialState = solutionHistory->getCurrentState();
263 RCP<Thyra::VectorBase<Scalar> > x = initialState->getX();
264 RCP<Thyra::VectorBase<Scalar> > xDot = initialState->getXDot();
266 auto inArgs = this->wrapperModel_->getNominalValues();
268 !((x != Teuchos::null && xDot != Teuchos::null) ||
269 (inArgs.get_x() != Teuchos::null &&
270 inArgs.get_x_dot() != Teuchos::null)),
272 "Error - We need to set the initial conditions for x and xDot from\n"
273 " either initialState or appModel_->getNominalValues::InArgs\n"
274 " (but not from a mixture of the two).\n");
277 if (x == Teuchos::null || xDot == Teuchos::null) {
278 using Teuchos::rcp_const_cast;
280 (inArgs.get_x() == Teuchos::null) ||
281 (inArgs.get_x_dot() == Teuchos::null),
283 "Error - setInitialConditions() needs the ICs from the initialState\n"
284 " or getNominalValues()!\n");
286 initialState->setX(x);
288 initialState->setXDot(xDot);
292 if (initialState->getXDotDot() == Teuchos::null)
293 initialState->setXDotDot(initialState->getX()->clone_v());
295 this->setStepperXDotDot(initialState->getXDotDot());
298 std::string icConsistency = this->getICConsistency();
299 if (icConsistency ==
"None") {
300 if (initialState->getXDotDot() == Teuchos::null) {
301 RCP<Teuchos::FancyOStream> out = this->getOStream();
302 out->setOutputToRootOnly(0);
304 out, 1,
"StepperNewmarkImplicitAForm::setInitialConditions()");
305 *out <<
"Warning -- Requested IC consistency of 'None' but\n"
306 <<
" initialState does not have an xDot.\n"
307 <<
" Setting a 'Zero' xDot!\n"
310 Thyra::assign(this->getStepperXDotDot(initialState).ptr(), Scalar(0.0));
313 else if (icConsistency ==
"Zero")
314 Thyra::assign(this->getStepperXDotDot(initialState).ptr(), Scalar(0.0));
315 else if (icConsistency ==
"App") {
317 inArgs.get_x_dot_dot());
319 xDotDot == Teuchos::null, std::logic_error,
320 "Error - setInitialConditions() requested 'App' for IC consistency,\n"
321 " but 'App' returned a null pointer for xDotDot!\n");
322 Thyra::assign(this->getStepperXDotDot(initialState).ptr(), *xDotDot);
324 else if (icConsistency ==
"Consistent") {
326 const Scalar time = initialState->getTime();
327 auto xDotDot = this->getStepperXDotDot(initialState);
331 if (this->initialGuess_ != Teuchos::null) {
333 !((xDotDot->space())->isCompatible(*this->initialGuess_->space())),
335 "Error - User-provided initial guess for Newton is not compatible\n"
336 " with solution vector!\n");
337 Thyra::copy(*this->initialGuess_, xDotDot.ptr());
340 Thyra::put_scalar(0.0, xDotDot.ptr());
345 this->wrapperModel_);
349 (*(this->solver_)).solve(&*xDotDot);
353 "Error - Solver failed while determining the initial conditions.\n"
359 true, std::logic_error,
360 "Error - setInitialConditions() invalid IC consistency, "
361 << icConsistency <<
".\n");
366 initialState->setIsSynced(
true);
369 if (this->getICConsistencyCheck()) {
370 auto f = initialState->getX()->clone_v();
371 auto xDotDot = this->getStepperXDotDot(initialState);
374 MEB::InArgs<Scalar> appInArgs =
375 this->wrapperModel_->getAppModel()->createInArgs();
376 MEB::OutArgs<Scalar> appOutArgs =
377 this->wrapperModel_->getAppModel()->createOutArgs();
380 appInArgs.set_x_dot(xDot);
381 appInArgs.set_x_dot_dot(xDotDot);
383 appOutArgs.set_f(appOutArgs.get_f());
385 appInArgs.set_W_x_dot_dot_coeff(Scalar(0.0));
386 appInArgs.set_alpha(Scalar(0.0));
387 appInArgs.set_beta(Scalar(0.0));
389 appInArgs.set_t(initialState->getTime());
391 this->wrapperModel_->getAppModel()->evalModel(appInArgs, appOutArgs);
393 Scalar reldiff = Thyra::norm(*f);
394 Scalar normx = Thyra::norm(*x);
396 if (normx > eps * reldiff) reldiff /= normx;
399 RCP<Teuchos::FancyOStream> out = this->getOStream();
400 out->setOutputToRootOnly(0);
402 out, 1,
"StepperNewmarkImplicitAForm::setInitialConditions()");
403 *out <<
"Warning -- Failed consistency check but continuing!\n"
404 <<
" ||f(x,xDot,xDotDot,t)||/||x|| > eps" << std::endl
405 <<
" ||f(x,xDot,xDotDot,t)|| = " << Thyra::norm(*f)
407 <<
" ||x|| = " << Thyra::norm(*x)
409 <<
" ||f(x,xDot,xDotDot,t)||/||x|| = " << reldiff << std::endl
410 <<
" eps = " << eps << std::endl;
414 if (!(this->getUseFSAL())) {
415 RCP<Teuchos::FancyOStream> out = this->getOStream();
416 out->setOutputToRootOnly(0);
418 "StepperNewmarkImplicitAForm::setInitialConditions()");
419 *out <<
"\nWarning -- The First-Same-As-Last (FSAL) principle is "
420 <<
"part of the Newmark Implicit A-Form. The default is to "
421 <<
"set useFSAL=true, and useFSAL=false will be ignored." << std::endl;
425 template <
class Scalar>
429 #ifdef VERBOSE_DEBUG_OUTPUT
430 *out_ <<
"DEBUG: " << __PRETTY_FUNCTION__ <<
"\n";
432 this->checkInitialized();
436 TEMPUS_FUNC_TIME_MONITOR(
"Tempus::StepperNewmarkImplicitAForm::takeStep()");
439 solutionHistory->getNumStates() < 2, std::logic_error,
440 "Error - StepperNewmarkImplicitAForm<Scalar>::takeStep(...)\n"
441 <<
"Need at least two SolutionStates for NewmarkImplicitAForm.\n"
442 <<
" Number of States = " << solutionHistory->getNumStates()
443 <<
"\nTry setting in \"Solution History\" \"Storage Type\" = "
445 <<
" or \"Storage Type\" = \"Static\" and \"Storage Limit\" = "
448 auto thisStepper = Teuchos::rcpFromRef(*
this);
449 stepperNewmarkImpAppAction_->execute(
450 solutionHistory, thisStepper,
452 Scalar>::ACTION_LOCATION::BEGIN_STEP);
454 RCP<SolutionState<Scalar> > workingState =
455 solutionHistory->getWorkingState();
456 RCP<SolutionState<Scalar> > currentState =
457 solutionHistory->getCurrentState();
461 this->wrapperModel_);
464 RCP<const Thyra::VectorBase<Scalar> > d_old = currentState->getX();
465 RCP<const Thyra::VectorBase<Scalar> > v_old = currentState->getXDot();
466 RCP<Thyra::VectorBase<Scalar> > a_old = currentState->getXDotDot();
469 RCP<Thyra::VectorBase<Scalar> > d_new = workingState->getX();
470 RCP<Thyra::VectorBase<Scalar> > v_new = workingState->getXDot();
471 RCP<Thyra::VectorBase<Scalar> > a_new = workingState->getXDotDot();
474 const Scalar time = currentState->getTime();
475 const Scalar dt = workingState->getTimeStep();
476 Scalar t = time + dt;
479 predictDisplacement(*d_new, *d_old, *v_old, *a_old, dt);
480 predictVelocity(*v_new, *v_old, *a_old, dt);
483 wrapperModel->initializeNewmark(v_new, d_new, dt, t, beta_, gamma_);
485 stepperNewmarkImpAppAction_->execute(
486 solutionHistory, thisStepper,
488 Scalar>::ACTION_LOCATION::BEFORE_SOLVE);
490 if (this->getZeroInitialGuess())
495 (*(this->solver_)).solve(&*a_new);
497 stepperNewmarkImpAppAction_->execute(
498 solutionHistory, thisStepper,
500 Scalar>::ACTION_LOCATION::AFTER_SOLVE);
503 correctVelocity(*v_new, *v_new, *a_new, dt);
504 correctDisplacement(*d_new, *d_new, *a_new, dt);
506 workingState->setSolutionStatus(sStatus);
507 workingState->setOrder(this->getOrder());
508 workingState->computeNorms(currentState);
510 stepperNewmarkImpAppAction_->execute(
511 solutionHistory, thisStepper,
513 Scalar>::ACTION_LOCATION::END_STEP);
524 template <
class Scalar>
528 #ifdef VERBOSE_DEBUG_OUTPUT
529 *out_ <<
"DEBUG: " << __PRETTY_FUNCTION__ <<
"\n";
536 template <
class Scalar>
541 #ifdef VERBOSE_DEBUG_OUTPUT
542 *out_ <<
"DEBUG: " << __PRETTY_FUNCTION__ <<
"\n";
549 out <<
"--- StepperNewmarkImplicitAForm ---\n";
550 out <<
" schemeName_ = " << schemeName_ << std::endl;
551 out <<
" beta_ = " << beta_ << std::endl;
552 out <<
" gamma_ = " << gamma_ << std::endl;
553 out <<
"-----------------------------------" << std::endl;
556 template <
class Scalar>
561 bool isValidSetup =
true;
567 if (this->wrapperModel_->getAppModel() == Teuchos::null) {
568 isValidSetup =
false;
569 out <<
"The application ModelEvaluator is not set!\n";
572 if (this->wrapperModel_ == Teuchos::null) {
573 isValidSetup =
false;
574 out <<
"The wrapper ModelEvaluator is not set!\n";
577 if (this->solver_ == Teuchos::null) {
578 isValidSetup =
false;
579 out <<
"The solver is not set!\n";
582 if (this->stepperNewmarkImpAppAction_ == Teuchos::null) {
583 isValidSetup =
false;
584 out <<
"The Newmark Implicit A-Form AppAction is not set!\n";
590 template <
class Scalar>
594 #ifdef VERBOSE_DEBUG_OUTPUT
595 *out_ <<
"DEBUG: " << __PRETTY_FUNCTION__ <<
"\n";
597 auto pl = this->getValidParametersBasicImplicit();
599 auto newmarkPL = Teuchos::parameterList(
"Newmark Parameters");
600 newmarkPL->set<std::string>(
"Scheme Name", schemeName_);
601 newmarkPL->set<
double>(
"Beta", beta_);
602 newmarkPL->set<
double>(
"Gamma", gamma_);
603 pl->set(
"Newmark Parameters", *newmarkPL);
610 template <
class Scalar>
617 stepper->setStepperImplicitValues(pl);
619 if (pl != Teuchos::null) {
620 if (pl->
isSublist(
"Newmark Parameters")) {
621 auto newmarkPL = pl->
sublist(
"Newmark Parameters",
true);
622 std::string schemeName =
623 newmarkPL.
get<std::string>(
"Scheme Name",
"Average Acceleration");
624 stepper->setSchemeName(schemeName);
625 if (schemeName ==
"User Defined") {
626 stepper->setBeta(newmarkPL.get<
double>(
"Beta", 0.25));
627 stepper->setGamma(newmarkPL.get<
double>(
"Gamma", 0.5));
631 stepper->setSchemeName(
"Average Acceleration");
635 if (model != Teuchos::null) {
636 stepper->setModel(model);
637 stepper->initialize();
644 #endif // Tempus_StepperNewmarkImplicitAForm_impl_hpp
virtual RCP< const VectorSpaceBase< Scalar > > space() const =0
virtual void setSolver(Teuchos::RCP< Thyra::NonlinearSolverBase< Scalar > > solver) override
Set solver.
Teuchos::RCP< StepperNewmarkImplicitAForm< Scalar > > createStepperNewmarkImplicitAForm(const Teuchos::RCP< const Thyra::ModelEvaluator< Scalar > > &model, Teuchos::RCP< Teuchos::ParameterList > pl)
Nonmember constructor - ModelEvaluator and ParameterList.
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.
T & get(const std::string &name, T def_value)
#define TEUCHOS_TEST_FOR_EXCEPTION(throw_exception_test, Exception, msg)
void setStepperName(std::string s)
Set the stepper name.
virtual void initialize()
Initialize after construction and changing input parameters.
virtual void describe(Teuchos::FancyOStream &out, const Teuchos::EVerbosityLevel verbLevel) const override
A ModelEvaluator for residual evaluations given a state. This ModelEvaluator takes a state...
Thyra Base interface for time steppers.
StepperState is a simple class to hold state information about the stepper.
TEUCHOS_DEPRECATED RCP< T > rcp(T *p, Dealloc_T dealloc, bool owns_mem)
virtual void describe(Teuchos::FancyOStream &out, const Teuchos::EVerbosityLevel verbLevel) const
bool isSublist(const std::string &name) const
virtual void setDefaultSolver()
void setICConsistencyCheck(bool c)
TEUCHOSCORE_LIB_DLL_EXPORT std::string toString(const EVerbosityLevel verbLevel)
virtual void setZeroInitialGuess(bool zIG)
SolutionHistory is basically a container of SolutionStates. SolutionHistory maintains a collection of...
basic_FancyOStream & setOutputToRootOnly(const int rootRank)
void validSecondOrderODE_DAE(const Teuchos::RCP< const Thyra::ModelEvaluator< Scalar > > &model)
ParameterList & sublist(const std::string &name, bool mustAlreadyExist=false, const std::string &docString="")
void setStepperType(std::string s)
Set the stepper type.
void setICConsistency(std::string s)