https://mooseframework.inl.gov
Loading...
Searching...
No Matches
SolutionTimeAdaptiveDT.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 "FEProblem.h"
12#include "Transient.h"
13
14#include <chrono>
15
17
20{
22 params.addClassDescription("Compute simulation timestep based on actual solution time.");
23 params.addParam<Real>(
24 "percent_change", 0.1, "Fraction to change the timestep by. Should be between 0 and 1");
25 params.addParam<int>(
26 "initial_direction", 1, "Direction for the first step. 1 for up... -1 for down. ");
27 params.addParam<bool>("adapt_log", false, "Output adaptive time step log");
28 params.addRequiredParam<Real>("dt", "The timestep size between solves");
29
30 return params;
31}
32
34 : TimeStepper(parameters),
35 _direction(declareRestartableData<int>("direction", getParam<int>("initial_direction"))),
36 _percent_change(getParam<Real>("percent_change")),
37 _older_sol_time_vs_dt(
38 declareRestartableData<Real>("older_sol_time_vs_dt", std::numeric_limits<Real>::max())),
39 _old_sol_time_vs_dt(
40 declareRestartableData<Real>("old_sol_time_vs_dt", std::numeric_limits<Real>::max())),
41 _sol_time_vs_dt(
42 declareRestartableData<Real>("sol_time_vs_dt", std::numeric_limits<Real>::max())),
43 _adapt_log(getParam<bool>("adapt_log"))
44{
45 if ((_adapt_log) && (processor_id() == 0))
46 {
47 static const std::string log("adaptive_log");
48 _adaptive_log.open(log);
49 if (_adaptive_log.fail())
50 mooseError("Unable to open file ", log);
51 _adaptive_log << "Adaptive Times Step Log" << std::endl;
52 }
53}
54
56
57void
59{
60 auto elapsed_time = stepAndRecordElapsedTime();
61
62 // Take the maximum time over all processors so all processors compute and use the same dt
63 TimeStepper::_communicator.max(elapsed_time);
64
67 _sol_time_vs_dt = elapsed_time / _dt;
68}
69
70std::chrono::milliseconds::rep
72{
73 auto start = std::chrono::system_clock::now();
75 auto end = std::chrono::system_clock::now();
76 return std::chrono::duration_cast<std::chrono::milliseconds>(end - start).count();
77}
78
79Real
81{
82 return getParam<Real>("dt");
83}
84
85Real
87{
88 // Ratio grew so switch direction
90 {
91 _direction *= -1;
92
93 // Make sure we take at least two steps in this new direction
94 _old_sol_time_vs_dt = std::numeric_limits<Real>::max();
95 _older_sol_time_vs_dt = std::numeric_limits<Real>::max();
96 }
97
98 Real local_dt = _dt + _dt * _percent_change * _direction;
99
100 if ((_adapt_log) && (processor_id() == 0))
101 {
102 Real out_dt = getCurrentDT();
103 if (out_dt > _dt_max)
104 out_dt = _dt_max;
105
106 _adaptive_log << "***Time step: " << _t_step << ", time = " << _time + out_dt
107 << "\nCur DT: " << out_dt << "\nOlder Ratio: " << _older_sol_time_vs_dt
108 << "\nOld Ratio: " << _old_sol_time_vs_dt << "\nNew Ratio: " << _sol_time_vs_dt
109 << std::endl;
110 }
111
112 return local_dt;
113}
114
115void
117{
118 _console << "Solve failed... cutting timestep" << std::endl;
119 if (_adapt_log)
120 _adaptive_log << "Solve failed... cutting timestep" << std::endl;
121
122 // Reverse progression of quantities
125
127}
registerMooseObject("MooseApp", SolutionTimeAdaptiveDT)
void ErrorVector unsigned int
const ConsoleStream _console
An instance of helper class to write streams to the Console objects.
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.
void mooseError(Args &&... args) const
Emits an error prefixed with object name and type and optionally a file path to the top-level block p...
Definition MooseBase.h:271
SolutionTimeAdaptiveDT(const InputParameters &parameters)
virtual Real computeDT() override
Computes time step size after the initial time step.
virtual std::chrono::milliseconds::rep stepAndRecordElapsedTime()
Take a step and record the elapsed time.
int & _direction
Multiplier specifying the direction the timestep is currently going.
std::ofstream _adaptive_log
The filehandle to hold the log.
virtual void rejectStep() override
This gets called when time step is rejected.
virtual Real computeInitialDT() override
Computes time step size for the initial time step.
const Real _percent_change
Percentage to change the timestep by either way.
virtual void step() override
Take a time step.
static InputParameters validParams()
bool _adapt_log
Boolean to control whether a separate adapt log is written to a file.
Real & _older_sol_time_vs_dt
Ratios to control whether to increase or decrease the current timestep.
void max(const T &r, T &o, Request &req) const
Base class for time stepping.
Definition TimeStepper.h:23
Real & _dt_max
virtual void rejectStep()
This gets called when time step is rejected.
static InputParameters validParams()
Definition TimeStepper.C:16
virtual void step()
Take a time step.
Real getCurrentDT()
Get the current_dt.
Definition TimeStepper.h:85
Real & _time
Values from executioner.
const Parallel::Communicator & _communicator
processor_id_type processor_id() const