174 const StepperBase<Scalar>& stepper)
178 using Thyra::createMember;
180 RCP<Teuchos::FancyOStream> out = this->getOStream();
181 Teuchos::EVerbosityLevel verbLevel = this->getVerbLevel();
182 const bool doTrace = (as<int>(verbLevel) >= as<int>(Teuchos::VERB_HIGH));
183 Teuchos::OSTab ostab(out,1,
"initialize");
186 *out <<
"\nEntering " << this->Teuchos::Describable::description()
187 <<
"::initialize()...\n";
190 if (is_null(errWtVec_))
191 errWtVec_ = createMember(stepper.get_x_space());
192 setStepControlState_(BEFORE_FIRST_STEP);
195 *out <<
"\nLeaving " << this->Teuchos::Describable::description()
196 <<
"::initialize()...\n";
202 const StepperBase<Scalar>& stepper,
203 const Scalar& stepSize,
204 const StepSizeType& stepSizeType)
206 typedef Teuchos::ScalarTraits<Scalar> ST;
207 TEUCHOS_TEST_FOR_EXCEPTION(
208 !((stepControlState_ == UNINITIALIZED) ||
209 (stepControlState_ == BEFORE_FIRST_STEP) ||
210 (stepControlState_ == READY_FOR_NEXT_STEP) ||
211 (stepControlState_ == MID_STEP)), std::logic_error,
212 "Error: Invalid state (stepControlState_=" << toString(stepControlState_)
213 <<
") for FirstOrderErrorStepControlStrategy<Scalar>::setRequestedStepSize()\n");
215 TEUCHOS_TEST_FOR_EXCEPTION(
216 ((stepSizeType == STEP_TYPE_FIXED) && (stepSize == ST::zero())),
217 std::logic_error,
"Error, step size type == STEP_TYPE_FIXED, "
218 "but requested step size == 0!\n");
220 TEUCHOS_TEST_FOR_EXCEPTION(
221 ((stepSizeType == STEP_TYPE_FIXED) && (stepSize < minStepSize_)),
222 std::logic_error,
"Error, step size type == STEP_TYPE_FIXED, "
223 "and (stepSize="<<stepSize<<
") < (minStepSize="<<minStepSize_<<
")!\n");
225 TEUCHOS_TEST_FOR_EXCEPTION(
226 ((stepSizeType == STEP_TYPE_FIXED) && (stepSize > maxStepSize_)),
227 std::logic_error,
"Error, step size type == STEP_TYPE_FIXED, "
228 "and (stepSize="<<stepSize<<
") > (maxStepSize="<<maxStepSize_<<
")!\n");
230 if (stepControlState_ == UNINITIALIZED) initialize(stepper);
231 requestedStepSize_ = stepSize;
232 stepSizeType_ = stepSizeType;
237 const StepperBase<Scalar>& , Scalar* stepSize,
238 StepSizeType* stepSizeType,
int* )
240 TEUCHOS_TEST_FOR_EXCEPTION(!((stepControlState_ == BEFORE_FIRST_STEP) ||
241 (stepControlState_ == MID_STEP) ||
242 (stepControlState_ == READY_FOR_NEXT_STEP) ),
244 "Error: Invalid state (stepControlState_=" << toString(stepControlState_)
245 <<
") for FirstOrderErrorStepControlStrategy<Scalar>::nextStepSize()\n");
247 if (stepControlState_ == BEFORE_FIRST_STEP) {
248 if (initialStepSize_ == initialStepSizeDefault_)
249 initialStepSize_ = requestedStepSize_;
250 nextStepSize_ = initialStepSize_;
253 stepSizeType_ = *stepSizeType;
254 if (stepSizeType_ == STEP_TYPE_FIXED)
255 currentStepSize_ = requestedStepSize_;
257 currentStepSize_ = nextStepSize_;
260 currentStepSize_ = std::min(requestedStepSize_, currentStepSize_);
262 *stepSize = currentStepSize_;
263 setStepControlState_(MID_STEP);
284 const StepperBase<Scalar>& , Scalar* )
286 TEUCHOS_TEST_FOR_EXCEPTION(stepControlState_ != AFTER_CORRECTION,
288 "Error: Invalid state (stepControlState_=" << toString(stepControlState_)
289 <<
") for FirstOrderErrorStepControlStrategy<Scalar>::completeStep()\n");
293 Thyra::abs(*x_, Teuchos::ptrFromRef(*errWtVec_));
294 Thyra::Vt_S(Teuchos::ptrFromRef(*errWtVec_), errorRelativeTolerance_);
295 Thyra::Vp_S(Teuchos::ptrFromRef(*errWtVec_), errorAbsoluteTolerance_);
296 reciprocal(*errWtVec_, Teuchos::ptrFromRef(*errWtVec_));
297 typedef Teuchos::ScalarTraits<Scalar> ST;
299 Vt_StV(Teuchos::ptrFromRef(*errWtVec_), ST::one(), *errWtVec_);
301 int N = x_->space()->dim();
302 Vt_S(Teuchos::ptrFromRef(*errWtVec_), Teuchos::as<Scalar>(1.0/N));
303 double wrms = norm_2(*errWtVec_,(*dx_));
304 stepSizeFactor_ = sqrt(2.0/wrms);
308 bool return_status =
true;
311 if ( (stepSizeFactor_ < minStepSizeDecreaseFactor_) || solveStatus_ < 0 )
312 return_status =
false;
314 Teuchos::EVerbosityLevel verbLevel = this->getVerbLevel();
315 if ( Teuchos::as<int>(verbLevel) >= Teuchos::as<int>(Teuchos::VERB_HIGH) ||
316 (return_status ==
false &&
317 Teuchos::as<int>(verbLevel) != Teuchos::as<int>(Teuchos::VERB_NONE)) ) {
318 RCP<Teuchos::FancyOStream> out = this->getOStream();
319 Teuchos::OSTab ostab(out,1,
"acceptStep");
320 *out <<
"\n wrms = " << wrms <<
"\n"
321 <<
" stepSizeFactor_ = " << stepSizeFactor_ <<
"\n"
322 <<
" solveStatus_ = " << solveStatus_ <<
"\n"
323 <<
" minStepSizeDecreaseFactor_ = " << minStepSizeDecreaseFactor_
327 return(return_status);
332 const StepperBase<Scalar>& )
334 TEUCHOS_TEST_FOR_EXCEPTION(stepControlState_ != AFTER_CORRECTION,
336 "Error: Invalid state (stepControlState_=" << toString(stepControlState_)
337 <<
") for FirstOrderErrorStepControlStrategy<Scalar>::completeStep()\n");
339 setStepControlState_(READY_FOR_NEXT_STEP);
343 RCP<Teuchos::FancyOStream> out = this->getOStream();
344 Teuchos::EVerbosityLevel verbLevel = this->getVerbLevel();
345 Teuchos::OSTab ostab(out,1,
"rejectStep");
348 if ( as<int>(verbLevel) != as<int>(Teuchos::VERB_NONE) )
349 *out <<
"numStepFailures_ = " << numStepFailures_ <<
"\n";
350 if (numStepFailures_ > maxStepFailures_) {
351 *out <<
"Rythmos_FirstOrderErrorStepControlStrategy::rejectStep(...): "
352 <<
"Error: Too many step failures "
353 <<
"(numStepFailures="<<numStepFailures_
354 <<
") > (maxStepFailures="<<maxStepFailures_<<
")\n";
355 return (REP_ERR_FAIL);
359 if (stepSizeType_ == STEP_TYPE_FIXED) {
360 if ( as<int>(verbLevel) != as<int>(Teuchos::VERB_NONE) ) {
361 *out <<
"Rythmos_FirstOrderErrorStepControl::rejectStep(...): "
362 <<
"Error: Step failure with fixed step size.\n";
364 return (REP_ERR_FAIL);
367 nextStepSize_ = currentStepSize_*
368 std::max(stepSizeFactor_, minStepSizeDecreaseFactor_);
369 nextStepSize_ = std::max(nextStepSize_, minStepSize_);
370 nextStepSize_ = std::min(nextStepSize_, maxStepSize_);
372 AttemptedStepStatusFlag return_status = PREDICT_AGAIN;
374 if ( as<int>(verbLevel) != as<int>(Teuchos::VERB_NONE) ) {
375 *out <<
"Rythmos_FirstOrderErrorStepControl::rejectStep(...): Step failure.\n"
376 <<
" Current step size is "<< currentStepSize_ <<
".\n"
377 <<
" Reducing next step size to "<< nextStepSize_ <<
".\n";
380 return(return_status);
385 const StepperBase<Scalar>& )
387 TEUCHOS_TEST_FOR_EXCEPTION(stepControlState_ != AFTER_CORRECTION,
389 "Error: Invalid state (stepControlState_=" << toString(stepControlState_)
390 <<
") for FirstOrderErrorStepControlStrategy<Scalar>::completeStep()\n");
393 if (stepSizeType_ == STEP_TYPE_VARIABLE) {
395 if (numStepFailures_ == 0) {
396 nextStepSize_ *= std::max(minStepSizeDecreaseFactor_,
397 std::min(stepSizeFactor_, maxStepSizeIncreaseFactor_));
400 nextStepSize_ = currentStepSize_;
401 numStepFailures_ = std::max(numStepFailures_-1,0);
404 nextStepSize_ = std::max(nextStepSize_, minStepSize_);
405 nextStepSize_ = std::min(nextStepSize_, maxStepSize_);
407 Teuchos::EVerbosityLevel verbLevel = this->getVerbLevel();
408 if ( Teuchos::as<int>(verbLevel) >= Teuchos::as<int>(Teuchos::VERB_HIGH) ) {
409 RCP<Teuchos::FancyOStream> out = this->getOStream();
410 Teuchos::OSTab ostab(out,1,
"completeStep_");
411 *out <<
"nextStepSize_ = " << nextStepSize_ <<
"\n";
412 *out <<
"numStepFailures_ = " << numStepFailures_ <<
"\n";
414 setStepControlState_(READY_FOR_NEXT_STEP);
441 RCP<Teuchos::ParameterList>
const& paramList)
446 TEUCHOS_TEST_FOR_EXCEPT(paramList == Teuchos::null);
447 paramList->validateParameters(*this->getValidParameters(),0);
448 parameterList_ = paramList;
449 Teuchos::readVerboseObjectSublist(&*parameterList_,
this);
451 initialStepSize_ = parameterList_->get(initialStepSizeName_,
452 initialStepSizeDefault_);
453 minStepSize_ = parameterList_->get(minStepSizeName_, minStepSizeDefault_);
454 maxStepSize_ = parameterList_->get(maxStepSizeName_, maxStepSizeDefault_);
455 TEUCHOS_TEST_FOR_EXCEPTION(
456 !(minStepSize_ <= maxStepSize_), std::logic_error,
457 "Error: (minStepSize="<<minStepSize_
458 <<
") > (maxStepSize="<<maxStepSize_<<
")\n");
459 TEUCHOS_TEST_FOR_EXCEPTION(
460 !((minStepSize_ <= initialStepSize_) && (initialStepSize_ <= maxStepSize_)),
462 "Error: Initial Step Size is not within min/max range.\n"
463 <<
" (minStepSize="<<minStepSize_
464 <<
") > (initialStepSize="<<initialStepSize_<<
") or \n"
465 <<
" (maxStepSize="<<maxStepSize_
466 <<
") < (initialStepSize="<<initialStepSize_<<
")\n");
468 maxStepSizeIncreaseFactor_ =
469 parameterList_->get(maxStepSizeIncreaseFactorName_,
470 maxStepSizeIncreaseFactorDefault_);
471 TEUCHOS_TEST_FOR_EXCEPTION(
472 !(maxStepSizeIncreaseFactor_ > 0.0), std::logic_error,
473 "Error: (maxStepSizeIncreaseFactor="
474 <<maxStepSizeIncreaseFactor_<<
") <= 0.0\n");
476 minStepSizeDecreaseFactor_ =
477 parameterList_->get(minStepSizeDecreaseFactorName_,
478 minStepSizeDecreaseFactorDefault_);
479 TEUCHOS_TEST_FOR_EXCEPTION(
480 !(minStepSizeDecreaseFactor_ > 0.0), std::logic_error,
481 "Error: (minStepSizeDecreaseFactor="
482 <<minStepSizeDecreaseFactor_<<
") <= 0.0\n");
484 TEUCHOS_TEST_FOR_EXCEPTION(
485 !(minStepSizeDecreaseFactor_ < maxStepSizeIncreaseFactor_),
486 std::logic_error,
"Error: "
487 "(minStepSizeDecreaseFactor="<<minStepSizeDecreaseFactor_<<
") >="
488 "(maxStepSizeIncreaseFactor="<<maxStepSizeIncreaseFactor_<<
")\n");
490 maxStepFailures_ = parameterList_->get(maxStepFailuresName_,
491 maxStepFailuresDefault_);
492 TEUCHOS_TEST_FOR_EXCEPTION(
493 !(maxStepFailures_ >= 0), std::logic_error,
494 "Error: (maxStepFailures="<<maxStepFailures_<<
") < 0\n");
496 errorRelativeTolerance_ = parameterList_->get(errorRelativeToleranceName_,
497 errorRelativeToleranceDefault_);
498 TEUCHOS_TEST_FOR_EXCEPTION(
499 !(errorRelativeTolerance_ >= 0.0), std::logic_error,
500 "Error: (errorRelativeTolerance="<<errorRelativeTolerance_<<
") < 0.0\n");
502 errorAbsoluteTolerance_ = parameterList_->get(errorAbsoluteToleranceName_,
503 errorAbsoluteToleranceDefault_);
504 TEUCHOS_TEST_FOR_EXCEPTION(
505 !(errorAbsoluteTolerance_ >= 0.0), std::logic_error,
506 "Error: (errorAbsoluteTolerance="<<errorAbsoluteTolerance_<<
") < 0.0\n");
508 RCP<Teuchos::FancyOStream> out = this->getOStream();
509 Teuchos::EVerbosityLevel verbLevel = this->getVerbLevel();
510 Teuchos::OSTab ostab(out,1,
"setParameterList");
513 if ( as<int>(verbLevel) >= as<int>(Teuchos::VERB_HIGH) ) {
514 *out <<
"minStepSize_ = " << minStepSize_ <<
"\n";
515 *out <<
"maxStepSize_ = " << maxStepSize_ <<
"\n";
516 *out <<
"maxStepSizeIncreaseFactor_ = " << maxStepSizeIncreaseFactor_<<
"\n";
517 *out <<
"minStepSizeDecreaseFactor_ = " << minStepSizeDecreaseFactor_<<
"\n";
518 *out <<
"maxStepFailures_ = " << maxStepFailures_ <<
"\n";
519 *out <<
"errorRelativeTolerance_ = " << errorRelativeTolerance_ <<
"\n";
520 *out <<
"errorAbsoluteTolerance_ = " << errorAbsoluteTolerance_ <<
"\n";
528 static RCP<Teuchos::ParameterList> validPL;
529 if (is_null(validPL)) {
530 RCP<Teuchos::ParameterList> pl = Teuchos::parameterList();
532 pl->set(initialStepSizeName_,initialStepSizeDefault_,
"Initial step size.");
533 pl->set(minStepSizeName_, minStepSizeDefault_,
"Minimum step size.");
534 pl->set(maxStepSizeName_, maxStepSizeDefault_,
"Maximum step size.");
535 pl->set(maxStepSizeIncreaseFactorName_, maxStepSizeIncreaseFactorDefault_,
536 "The maximum factor to increase the step size after a successful step.");
537 pl->set(minStepSizeDecreaseFactorName_, minStepSizeDecreaseFactorDefault_,
538 "The minimum allowable factor to decrease the step size. If the "
539 "stepSizeFactor_ is below this, the current step is considered a "
540 "failure and retried with `" + minStepSizeDecreaseFactorName_ +
542 pl->set(maxStepFailuresName_, maxStepFailuresDefault_,
543 "The maximum number of step failures before exiting with an error. "
544 "The number of failure steps are carried between successful steps.");
545 pl->set(errorRelativeToleranceName_, errorRelativeToleranceDefault_,
546 "The allowable relative change in the error (the difference between "
547 "the solution at the end of the step and the predicted solution) for "
548 "each step to pass. The stepper solution status is also used to "
549 "determine pass/fail.");
550 pl->set(errorAbsoluteToleranceName_, errorAbsoluteToleranceDefault_,
551 "The allowable absolute change in the error (the difference between "
552 "the solution at the end of the step and the predicted solution) for "
553 "each step to pass. The stepper solution status is also used to "
554 "determine pass/fail.");
556 Teuchos::setupVerboseObjectSublist(&*pl);