https://mooseframework.inl.gov
Loading...
Searching...
No Matches
AB2PredictorCorrector.C
Go to the documentation of this file.
1//* This file is part of the MOOSE framework
2//* https://mooseframework.inl.gov
3//*
4//* All rights reserved, see COPYRIGHT for full restrictions
5//* https://github.com/idaholab/moose/blob/master/COPYRIGHT
6//*
7//* Licensed under LGPL 2.1, please see LICENSE for details
8//* https://www.gnu.org/licenses/lgpl-2.1.html
9
11#include "AdamsPredictor.h"
12#include "Problem.h"
13#include "FEProblem.h"
14#include "MooseApp.h"
15#include "NonlinearSystem.h"
16#include "AuxiliarySystem.h"
17#include "TimeIntegrator.h"
18#include "Conversion.h"
19
20#include "libmesh/nonlinear_solver.h"
21#include "libmesh/numeric_vector.h"
22
23// C++ Includes
24#include <iomanip>
25#include <iostream>
26#include <fstream>
27
29
32{
35 "Implements second order Adams-Bashforth method for timestep calculation.");
36 params.addRequiredParam<Real>("e_tol", "Target error tolerance.");
37 params.addRequiredParam<Real>("e_max", "Maximum acceptable error.");
38 params.addRequiredParam<Real>("dt", "Initial time step size");
39 params.addParam<Real>("max_increase", 1.0e9, "Maximum ratio that the time step can increase.");
40 params.addParam<int>(
41 "steps_between_increase", 1, "the number of time steps before recalculating dt");
42 params.addParam<int>("start_adapting", 2, "when to start taking adaptive time steps");
43 params.addParam<Real>("scaling_parameter", .8, "scaling parameter for dt selection");
44 return params;
45}
46
48 : TimeStepper(parameters),
49 _u1(_fe_problem.getNonlinearSystemBase(/*nl_sys=*/0).addVector("u1", true, GHOSTED)),
50 _aux1(_fe_problem.getAuxiliarySystem().addVector("aux1", true, GHOSTED)),
51 _pred1(_fe_problem.getNonlinearSystemBase(/*nl_sys=*/0).addVector("pred1", true, GHOSTED)),
52 _dt_full(declareRestartableData<Real>("dt_full", 0)),
53 _error(declareRestartableData<Real>("error", 0)),
54 _e_tol(getParam<Real>("e_tol")),
55 _e_max(getParam<Real>("e_max")),
56 _max_increase(getParam<Real>("max_increase")),
57 _steps_between_increase(getParam<int>("steps_between_increase")),
58 _dt_steps_taken(declareRestartableData<int>("dt_steps_taken", 0)),
59 _start_adapting(getParam<int>("start_adapting")),
60 _my_dt_old(declareRestartableData<Real>("my_dt_old", 0)),
61 _infnorm(declareRestartableData<Real>("infnorm", 0)),
62 _scaling_parameter(getParam<Real>("scaling_parameter"))
63{
64 Real predscale = 1.;
65 InputParameters params = _app.getFactory().getValidParams("AdamsPredictor");
66 params.set<Real>("scale") = predscale;
67 _fe_problem.addPredictor("AdamsPredictor", "adamspredictor", params);
68}
69
70void
75
76void
78{
79 // save dt
80 _dt_full = _dt;
81}
82
83void
85{
88
89 _fe_problem.solve(/*nl_sys=*/0);
90 _converged = _fe_problem.converged(/*nl_sys=*/0);
91 if (_converged)
92 {
93 _u1 = *nl.currentSolution();
94 _u1.close();
95
96 _aux1 = *aux.currentSolution();
97 _aux1.close();
99 {
100 // Calculate error if past the first solve
102
104 _e_max = 1.1 * _e_tol * _infnorm;
105 _console << "Time Error Estimate: " << _error << std::endl;
106 }
107 else
108 {
109 // First time step is problematic, sure we converged but what does that mean? We don't know.
110 // Nor can we calculate the error on the first time step.
111 }
112 }
113}
114
115bool
117{
118 if (!_converged)
119 return false;
120 if (_error < _e_max)
121 return true;
122 else
123 return false;
124}
125
126void
128{
129 if (!converged())
130 _dt_steps_taken = 0;
131
132 if (_error >= _e_max)
133 _console << "Marking last solve not converged " << _error << " " << _e_max << std::endl;
134}
135
136Real
138{
140 return _dt;
141
142 _my_dt_old = _dt;
143
144 _dt_steps_taken += 1;
146 {
147
148 Real new_dt = _dt_full * _scaling_parameter * std::pow(_infnorm * _e_tol / _error, 1.0 / 3.0);
149
150 if (new_dt / _dt_full > _max_increase)
151 new_dt = _dt_full * _max_increase;
152 _dt_steps_taken = 0;
153 return new_dt;
154 }
155
156 return _dt;
157}
158
159Real
161{
162 return getParam<Real>("dt");
163}
164
165Real
166AB2PredictorCorrector::estimateTimeError(NumericVector<Number> & solution)
167{
169 const auto & ti =
170 _fe_problem.getNonlinearSystemBase(/*nl_sys=*/0).getTimeIntegrator(/*var_num=*/0);
171
172 auto scheme = Moose::stringToEnum<Moose::TimeIntegratorType>(ti.type());
173 Real dt_old = _my_dt_old;
174 if (dt_old == 0)
175 dt_old = _dt;
176
177 switch (scheme)
178 {
180 {
181 _pred1 *= -1;
182 _pred1 += solution;
183 Real calc = _dt * _dt * .5;
184 _pred1 *= calc;
185 return _pred1.l2_norm();
186 }
188 {
189 _pred1 -= solution;
190 _pred1 *= (_dt) / (3.0 * (_dt + dt_old));
191 return _pred1.l2_norm();
192 }
193 case Moose::TI_BDF2:
194 {
195 _pred1 *= -1.0;
196 _pred1 += solution;
197 Real topcalc = 2.0 * (_dt + dt_old) * (_dt + dt_old);
198 Real bottomcalc = 6.0 * _dt * _dt + 12.0 * _dt * dt_old + 5.0 * dt_old * dt_old;
199 _pred1 *= topcalc / bottomcalc;
200
201 return _pred1.l2_norm();
202 }
203 default:
204 break;
205 }
206 return -1;
207}
registerMooseObject("MooseApp", AB2PredictorCorrector)
void ErrorVector unsigned int
A TimeStepper based on the AB2 method.
virtual void preExecute() override
int _steps_between_increase
steps to take before increasing dt
virtual Real computeDT() override
Computes time step size after the initial time step.
virtual void preSolve() override
virtual void step() override
Take a time step.
virtual void postSolve() override
virtual bool converged() const override
If the time step converged.
Real _e_tol
error tolerance
NumericVector< Number > & _pred1
static InputParameters validParams()
Real & _error
global relative time discretization error estimate
Real & _dt_full
dt of the big step
AB2PredictorCorrector(const InputParameters &parameters)
Real _scaling_parameter
scaling_parameter for time step selection, default is 0.8
Real & _infnorm
infinity norm of the solution vector
virtual Real computeInitialDT() override
Computes time step size for the initial time step.
Real _max_increase
maximum increase ratio
virtual Real estimateTimeError(NumericVector< Number > &sol)
NumericVector< Number > & _aux1
NumericVector< Number > & _u1
int & _dt_steps_taken
steps taken at current dt
A system that holds auxiliary variables.
const NumericVector< Number > *const & currentSolution() const override
The solution vector that is currently being operated on.
const ConsoleStream _console
An instance of helper class to write streams to the Console objects.
virtual void addPredictor(const std::string &type, const std::string &name, InputParameters &parameters)
AuxiliarySystem & getAuxiliarySystem()
virtual void solve(const unsigned int nl_sys_num)
NonlinearSystemBase & getNonlinearSystemBase(const unsigned int sys_num)
InputParameters getValidParams(const std::string &name) const
Get valid parameters for the object.
Definition Factory.C:68
The main MOOSE class responsible for handling user-defined parameters in almost every MOOSE system.
void addParam(const std::string &name, const S &value, const std::string &doc_string)
These methods add an optional parameter and a documentation string to the InputParameters object.
void addRequiredParam(const std::string &name, const std::string &doc_string)
This method adds a parameter and documentation string to the InputParameters object that will be extr...
void addClassDescription(const std::string &doc_string)
This method adds a description of the class that will be displayed in the input file syntax dump.
T & set(const std::string &name, bool quiet_mode=false)
Returns a writable reference to the named parameters.
Factory & getFactory()
Retrieve a writable reference to the Factory associated with this App.
Definition MooseApp.h:407
MooseApp & _app
The MOOSE application this is associated with.
Definition MooseBase.h:375
Nonlinear system to be solved.
virtual NumericVector< Number > & solutionPredictor()
Definition Predictor.h:41
virtual const NumericVector< Number > *const & currentSolution() const override final
The solution vector that is currently being operated on.
virtual bool converged(const unsigned int sys_num)
Eventually we want to convert this virtual over to taking a solver system number argument.
Definition SubProblem.h:113
const TimeIntegrator & getTimeIntegrator(const unsigned int var_num) const
Retrieve the time integrator that integrates the given variable's equation.
Base class for time stepping.
Definition TimeStepper.h:23
FEProblemBase & _fe_problem
bool _converged
Whether or not the previous solve converged.
static InputParameters validParams()
Definition TimeStepper.C:16
virtual void preExecute()
Definition TimeStepper.C:71
virtual void close()=0
virtual Real l2_norm() const=0
virtual Real linfty_norm() const=0
@ TI_IMPLICIT_EULER
Definition MooseTypes.h:958
@ TI_CRANK_NICOLSON
Definition MooseTypes.h:960
@ TI_BDF2
Definition MooseTypes.h:961
MooseUnits pow(const MooseUnits &, int)
Definition Units.C:537