9 #ifndef Tempus_StepperNewmarkImplicitAForm_impl_hpp
10 #define Tempus_StepperNewmarkImplicitAForm_impl_hpp
19 template <
class Scalar>
24 #ifdef VERBOSE_DEBUG_OUTPUT
25 *out_ <<
"DEBUG: " << __PRETTY_FUNCTION__ <<
"\n";
28 Thyra::V_StVpStV(Teuchos::ptrFromRef(vPred), 1.0, v, dt * (1.0 - gamma_), a);
31 template <
class Scalar>
35 const Scalar dt)
const
37 #ifdef VERBOSE_DEBUG_OUTPUT
38 *out_ <<
"DEBUG: " << __PRETTY_FUNCTION__ <<
"\n";
41 Thyra::createMember<Scalar>(dPred.
space());
43 Scalar aConst = dt * dt / 2.0 * (1.0 - 2.0 * beta_);
44 Thyra::V_StVpStV(Teuchos::ptrFromRef(dPred), dt, v, aConst, a);
46 Thyra::Vp_V(Teuchos::ptrFromRef(dPred), d, 1.0);
49 template <
class Scalar>
54 #ifdef VERBOSE_DEBUG_OUTPUT
55 *out_ <<
"DEBUG: " << __PRETTY_FUNCTION__ <<
"\n";
58 Thyra::V_StVpStV(Teuchos::ptrFromRef(v), 1.0, vPred, dt * gamma_, a);
61 template <
class Scalar>
66 #ifdef VERBOSE_DEBUG_OUTPUT
67 *out_ <<
"DEBUG: " << __PRETTY_FUNCTION__ <<
"\n";
70 Thyra::V_StVpStV(Teuchos::ptrFromRef(d), 1.0, dPred, beta_ * dt * dt, a);
73 template <
class Scalar>
76 if (schemeName_ !=
"User Defined") {
77 *out_ <<
"\nWARNING: schemeName != 'User Defined' (=" << schemeName_
79 <<
" Not setting beta, and leaving as beta = " << beta_ <<
"!\n";
86 *out_ <<
"\nWARNING: Running (implicit implementation of) Newmark "
87 <<
"Implicit a-Form Stepper with Beta = 0.0, which \n"
88 <<
"specifies an explicit scheme. Mass lumping is not possible, "
89 <<
"so this will be slow! To run explicit \n"
90 <<
"implementation of Newmark Implicit a-Form Stepper, please "
91 <<
"re-run with 'Stepper Type' = 'Newmark Explicit a-Form'.\n"
92 <<
"This stepper allows for mass lumping when called through "
93 <<
"Piro::TempusSolver.\n";
97 (beta_ > 1.0) || (beta_ < 0.0), std::logic_error,
98 "\nError in 'Newmark Implicit a-Form' stepper: invalid value of Beta = "
99 << beta_ <<
". Please select Beta >= 0 and <= 1. \n");
102 template <
class Scalar>
105 if (schemeName_ !=
"User Defined") {
106 *out_ <<
"\nWARNING: schemeName != 'User Defined' (=" << schemeName_
108 <<
" Not setting gamma, and leaving as gamma = " << gamma_ <<
"!\n";
115 (gamma_ > 1.0) || (gamma_ < 0.0), std::logic_error,
116 "\nError in 'Newmark Implicit a-Form' stepper: invalid value of Gamma ="
117 << gamma_ <<
". Please select Gamma >= 0 and <= 1. \n");
120 template <
class Scalar>
123 schemeName_ = schemeName;
125 if (schemeName_ ==
"Average Acceleration") {
129 else if (schemeName_ ==
"Linear Acceleration") {
133 else if (schemeName_ ==
"Central Difference") {
137 else if (schemeName_ ==
"User Defined") {
143 true, std::logic_error,
144 "\nError in Tempus::StepperNewmarkImplicitAForm! "
145 <<
"Invalid Scheme Name = " << schemeName_ <<
". \n"
146 <<
"Valid Scheme Names are: 'Average Acceleration', "
147 <<
"'Linear Acceleration', \n"
148 <<
"'Central Difference' and 'User Defined'.\n");
151 this->isInitialized_ =
false;
154 template <
class Scalar>
158 if (appAction == Teuchos::null) {
160 stepperNewmarkImpAppAction_ =
164 stepperNewmarkImpAppAction_ = appAction;
167 this->isInitialized_ =
false;
170 template <
class Scalar>
172 : out_(Teuchos::VerboseObjectBase::getDefaultOStream())
174 #ifdef VERBOSE_DEBUG_OUTPUT
175 *
out_ <<
"DEBUG: " << __PRETTY_FUNCTION__ <<
"\n";
190 template <
class Scalar>
194 bool useFSAL, std::string ICConsistency,
bool ICConsistencyCheck,
195 bool zeroInitialGuess, std::string schemeName, Scalar beta, Scalar gamma,
198 : out_(Teuchos::VerboseObjectBase::getDefaultOStream())
212 if (appModel != Teuchos::null) {
218 template <
class Scalar>
222 #ifdef VERBOSE_DEBUG_OUTPUT
223 *out_ <<
"DEBUG: " << __PRETTY_FUNCTION__ <<
"\n";
226 this->wrapperModel_ =
228 appModel,
"Newmark Implicit a-Form"));
231 std::logic_error,
"Error - Solver is not set!\n");
232 this->getSolver()->setModel(this->wrapperModel_);
234 this->isInitialized_ =
false;
237 template <
class Scalar>
243 int numStates = solutionHistory->getNumStates();
246 numStates < 1, std::logic_error,
247 "Error - setInitialConditions() needs at least one SolutionState\n"
248 " to set the initial condition. Number of States = "
252 RCP<Teuchos::FancyOStream> out = this->getOStream();
253 out->setOutputToRootOnly(0);
255 "StepperNewmarkImplicitAForm::setInitialConditions()");
256 *out <<
"Warning -- SolutionHistory has more than one state!\n"
257 <<
"Setting the initial conditions on the currentState.\n"
261 RCP<SolutionState<Scalar> > initialState = solutionHistory->getCurrentState();
262 RCP<Thyra::VectorBase<Scalar> > x = initialState->getX();
263 RCP<Thyra::VectorBase<Scalar> > xDot = initialState->getXDot();
265 auto inArgs = this->wrapperModel_->getNominalValues();
267 !((x != Teuchos::null && xDot != Teuchos::null) ||
268 (inArgs.get_x() != Teuchos::null &&
269 inArgs.get_x_dot() != Teuchos::null)),
271 "Error - We need to set the initial conditions for x and xDot from\n"
272 " either initialState or appModel_->getNominalValues::InArgs\n"
273 " (but not from a mixture of the two).\n");
276 if (x == Teuchos::null || xDot == Teuchos::null) {
277 using Teuchos::rcp_const_cast;
279 (inArgs.get_x() == Teuchos::null) ||
280 (inArgs.get_x_dot() == Teuchos::null),
282 "Error - setInitialConditions() needs the ICs from the initialState\n"
283 " or getNominalValues()!\n");
285 initialState->setX(x);
287 initialState->setXDot(xDot);
291 if (initialState->getXDotDot() == Teuchos::null)
292 initialState->setXDotDot(initialState->getX()->clone_v());
294 this->setStepperXDotDot(initialState->getXDotDot());
297 std::string icConsistency = this->getICConsistency();
298 if (icConsistency ==
"None") {
299 if (initialState->getXDotDot() == Teuchos::null) {
300 RCP<Teuchos::FancyOStream> out = this->getOStream();
301 out->setOutputToRootOnly(0);
303 out, 1,
"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"
309 Thyra::assign(this->getStepperXDotDot(initialState).ptr(), Scalar(0.0));
312 else if (icConsistency ==
"Zero")
313 Thyra::assign(this->getStepperXDotDot(initialState).ptr(), Scalar(0.0));
314 else if (icConsistency ==
"App") {
316 inArgs.get_x_dot_dot());
318 xDotDot == Teuchos::null, std::logic_error,
319 "Error - setInitialConditions() requested 'App' for IC consistency,\n"
320 " but 'App' returned a null pointer for xDotDot!\n");
321 Thyra::assign(this->getStepperXDotDot(initialState).ptr(), *xDotDot);
323 else if (icConsistency ==
"Consistent") {
325 const Scalar time = initialState->getTime();
326 auto xDotDot = this->getStepperXDotDot(initialState);
330 if (this->initialGuess_ != Teuchos::null) {
332 !((xDotDot->space())->isCompatible(*this->initialGuess_->space())),
334 "Error - User-provided initial guess for Newton is not compatible\n"
335 " with solution vector!\n");
336 Thyra::copy(*this->initialGuess_, xDotDot.ptr());
339 Thyra::put_scalar(0.0, xDotDot.ptr());
344 this->wrapperModel_);
348 (*(this->solver_)).solve(&*xDotDot);
352 "Error - Solver failed while determining the initial conditions.\n"
358 true, std::logic_error,
359 "Error - setInitialConditions() invalid IC consistency, "
360 << icConsistency <<
".\n");
365 initialState->setIsSynced(
true);
368 if (this->getICConsistencyCheck()) {
369 auto f = initialState->getX()->clone_v();
370 auto xDotDot = this->getStepperXDotDot(initialState);
373 MEB::InArgs<Scalar> appInArgs =
374 this->wrapperModel_->getAppModel()->createInArgs();
375 MEB::OutArgs<Scalar> appOutArgs =
376 this->wrapperModel_->getAppModel()->createOutArgs();
379 appInArgs.set_x_dot(xDot);
380 appInArgs.set_x_dot_dot(xDotDot);
382 appOutArgs.set_f(appOutArgs.get_f());
384 appInArgs.set_W_x_dot_dot_coeff(Scalar(0.0));
385 appInArgs.set_alpha(Scalar(0.0));
386 appInArgs.set_beta(Scalar(0.0));
388 appInArgs.set_t(initialState->getTime());
390 this->wrapperModel_->getAppModel()->evalModel(appInArgs, appOutArgs);
392 Scalar reldiff = Thyra::norm(*f);
393 Scalar normx = Thyra::norm(*x);
395 if (normx > eps * reldiff) reldiff /= normx;
398 RCP<Teuchos::FancyOStream> out = this->getOStream();
399 out->setOutputToRootOnly(0);
401 out, 1,
"StepperNewmarkImplicitAForm::setInitialConditions()");
402 *out <<
"Warning -- Failed consistency check but continuing!\n"
403 <<
" ||f(x,xDot,xDotDot,t)||/||x|| > eps" << std::endl
404 <<
" ||f(x,xDot,xDotDot,t)|| = " << Thyra::norm(*f)
406 <<
" ||x|| = " << Thyra::norm(*x)
408 <<
" ||f(x,xDot,xDotDot,t)||/||x|| = " << reldiff << std::endl
409 <<
" eps = " << eps << std::endl;
413 if (!(this->getUseFSAL())) {
414 RCP<Teuchos::FancyOStream> out = this->getOStream();
415 out->setOutputToRootOnly(0);
417 "StepperNewmarkImplicitAForm::setInitialConditions()");
418 *out <<
"\nWarning -- The First-Same-As-Last (FSAL) principle is "
419 <<
"part of the Newmark Implicit A-Form. The default is to "
420 <<
"set useFSAL=true, and useFSAL=false will be ignored." << std::endl;
424 template <
class Scalar>
428 #ifdef VERBOSE_DEBUG_OUTPUT
429 *out_ <<
"DEBUG: " << __PRETTY_FUNCTION__ <<
"\n";
431 this->checkInitialized();
435 TEMPUS_FUNC_TIME_MONITOR(
"Tempus::StepperNewmarkImplicitAForm::takeStep()");
438 solutionHistory->getNumStates() < 2, std::logic_error,
439 "Error - StepperNewmarkImplicitAForm<Scalar>::takeStep(...)\n"
440 <<
"Need at least two SolutionStates for NewmarkImplicitAForm.\n"
441 <<
" Number of States = " << solutionHistory->getNumStates()
442 <<
"\nTry setting in \"Solution History\" \"Storage Type\" = "
444 <<
" or \"Storage Type\" = \"Static\" and \"Storage Limit\" = "
447 auto thisStepper = Teuchos::rcpFromRef(*
this);
448 stepperNewmarkImpAppAction_->execute(
449 solutionHistory, thisStepper,
451 Scalar>::ACTION_LOCATION::BEGIN_STEP);
453 RCP<SolutionState<Scalar> > workingState =
454 solutionHistory->getWorkingState();
455 RCP<SolutionState<Scalar> > currentState =
456 solutionHistory->getCurrentState();
460 this->wrapperModel_);
463 RCP<const Thyra::VectorBase<Scalar> > d_old = currentState->getX();
464 RCP<const Thyra::VectorBase<Scalar> > v_old = currentState->getXDot();
465 RCP<Thyra::VectorBase<Scalar> > a_old = currentState->getXDotDot();
468 RCP<Thyra::VectorBase<Scalar> > d_new = workingState->getX();
469 RCP<Thyra::VectorBase<Scalar> > v_new = workingState->getXDot();
470 RCP<Thyra::VectorBase<Scalar> > a_new = workingState->getXDotDot();
473 const Scalar time = currentState->getTime();
474 const Scalar dt = workingState->getTimeStep();
475 Scalar t = time + dt;
478 predictDisplacement(*d_new, *d_old, *v_old, *a_old, dt);
479 predictVelocity(*v_new, *v_old, *a_old, dt);
482 wrapperModel->initializeNewmark(v_new, d_new, dt, t, beta_, gamma_);
484 stepperNewmarkImpAppAction_->execute(
485 solutionHistory, thisStepper,
487 Scalar>::ACTION_LOCATION::BEFORE_SOLVE);
489 if (this->getZeroInitialGuess())
494 (*(this->solver_)).solve(&*a_new);
496 stepperNewmarkImpAppAction_->execute(
497 solutionHistory, thisStepper,
499 Scalar>::ACTION_LOCATION::AFTER_SOLVE);
502 correctVelocity(*v_new, *v_new, *a_new, dt);
503 correctDisplacement(*d_new, *d_new, *a_new, dt);
505 workingState->setSolutionStatus(sStatus);
506 workingState->setOrder(this->getOrder());
507 workingState->computeNorms(currentState);
509 stepperNewmarkImpAppAction_->execute(
510 solutionHistory, thisStepper,
512 Scalar>::ACTION_LOCATION::END_STEP);
523 template <
class Scalar>
527 #ifdef VERBOSE_DEBUG_OUTPUT
528 *out_ <<
"DEBUG: " << __PRETTY_FUNCTION__ <<
"\n";
535 template <
class Scalar>
540 #ifdef VERBOSE_DEBUG_OUTPUT
541 *out_ <<
"DEBUG: " << __PRETTY_FUNCTION__ <<
"\n";
548 out <<
"--- StepperNewmarkImplicitAForm ---\n";
549 out <<
" schemeName_ = " << schemeName_ << std::endl;
550 out <<
" beta_ = " << beta_ << std::endl;
551 out <<
" gamma_ = " << gamma_ << std::endl;
552 out <<
"-----------------------------------" << std::endl;
555 template <
class Scalar>
560 bool isValidSetup =
true;
566 if (this->wrapperModel_->getAppModel() == Teuchos::null) {
567 isValidSetup =
false;
568 out <<
"The application ModelEvaluator is not set!\n";
571 if (this->wrapperModel_ == Teuchos::null) {
572 isValidSetup =
false;
573 out <<
"The wrapper ModelEvaluator is not set!\n";
576 if (this->solver_ == Teuchos::null) {
577 isValidSetup =
false;
578 out <<
"The solver is not set!\n";
581 if (this->stepperNewmarkImpAppAction_ == Teuchos::null) {
582 isValidSetup =
false;
583 out <<
"The Newmark Implicit A-Form AppAction is not set!\n";
589 template <
class Scalar>
593 #ifdef VERBOSE_DEBUG_OUTPUT
594 *out_ <<
"DEBUG: " << __PRETTY_FUNCTION__ <<
"\n";
596 auto pl = this->getValidParametersBasicImplicit();
598 auto newmarkPL = Teuchos::parameterList(
"Newmark Parameters");
599 newmarkPL->set<std::string>(
"Scheme Name", schemeName_);
600 newmarkPL->set<
double>(
"Beta", beta_);
601 newmarkPL->set<
double>(
"Gamma", gamma_);
602 pl->set(
"Newmark Parameters", *newmarkPL);
609 template <
class Scalar>
616 stepper->setStepperImplicitValues(pl);
618 if (pl != Teuchos::null) {
619 if (pl->
isSublist(
"Newmark Parameters")) {
620 auto newmarkPL = pl->
sublist(
"Newmark Parameters",
true);
621 std::string schemeName =
622 newmarkPL.
get<std::string>(
"Scheme Name",
"Average Acceleration");
623 stepper->setSchemeName(schemeName);
624 if (schemeName ==
"User Defined") {
625 stepper->setBeta(newmarkPL.get<
double>(
"Beta", 0.25));
626 stepper->setGamma(newmarkPL.get<
double>(
"Gamma", 0.5));
630 stepper->setSchemeName(
"Average Acceleration");
634 if (model != Teuchos::null) {
635 stepper->setModel(model);
636 stepper->initialize();
643 #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)