www.mooseframework.org
SolutionTimeAdaptiveDT.C
Go to the documentation of this file.
1 //* This file is part of the MOOSE framework
2 //* https://www.mooseframework.org
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 
10 #include "SolutionTimeAdaptiveDT.h"
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(getParam<int>("initial_direction")),
36  _percent_change(getParam<Real>("percent_change")),
37  _older_sol_time_vs_dt(std::numeric_limits<Real>::max()),
38  _old_sol_time_vs_dt(std::numeric_limits<Real>::max()),
39  _sol_time_vs_dt(std::numeric_limits<Real>::max()),
40  _adapt_log(getParam<bool>("adapt_log"))
41 
42 {
43  if ((_adapt_log) && (processor_id() == 0))
44  {
45  static const std::string log("adaptive_log");
46  _adaptive_log.open(log);
47  if (_adaptive_log.fail())
48  mooseError("Unable to open file ", log);
49  _adaptive_log << "Adaptive Times Step Log" << std::endl;
50  }
51 }
52 
54 
55 void
57 {
58  auto solve_start = std::chrono::system_clock::now();
59 
61 
62  if (converged())
63  {
64  auto solve_end = std::chrono::system_clock::now();
65  auto elapsed_time =
66  std::chrono::duration_cast<std::chrono::milliseconds>(solve_end - solve_start).count();
67 
68  // Take the maximum time over all processors so all processors compute and use the same dt
69  TimeStepper::_communicator.max(elapsed_time);
70 
73  _sol_time_vs_dt = elapsed_time / _dt;
74  }
75 }
76 
77 Real
79 {
80  return getParam<Real>("dt");
81 }
82 
83 Real
85 {
86  // Ratio grew so switch direction
88  {
89  _direction *= -1;
90 
91  // Make sure we take at least two steps in this new direction
94  }
95 
96  Real local_dt = _dt + _dt * _percent_change * _direction;
97 
98  if ((_adapt_log) && (processor_id() == 0))
99  {
100  Real out_dt = getCurrentDT();
101  if (out_dt > _dt_max)
102  out_dt = _dt_max;
103 
104  _adaptive_log << "***Time step: " << _t_step << ", time = " << _time + out_dt
105  << "\nCur DT: " << out_dt << "\nOlder Ratio: " << _older_sol_time_vs_dt
106  << "\nOld Ratio: " << _old_sol_time_vs_dt << "\nNew Ratio: " << _sol_time_vs_dt
107  << std::endl;
108  }
109 
110  return local_dt;
111 }
112 
113 void
115 {
116  _console << "Solve failed... cutting timestep" << std::endl;
117  if (_adapt_log)
118  _adaptive_log << "Solve failed... cutting timestep" << std::endl;
119 
121 }
static InputParameters validParams()
Definition: TimeStepper.C:16
Real _percent_change
Percentage to change the timestep by either way.
Real _older_sol_time_vs_dt
Ratios to control whether to increase or decrease the current timestep.
registerMooseObject("MooseApp", SolutionTimeAdaptiveDT)
bool _adapt_log
Boolean to control whether a separate adapt log is written to a file.
Base class for time stepping.
Definition: TimeStepper.h:22
The main MOOSE class responsible for handling user-defined parameters in almost every MOOSE system...
static InputParameters validParams()
const Parallel::Communicator & _communicator
virtual bool converged() const
If the time step converged.
Definition: TimeStepper.C:197
std::ofstream _adaptive_log
The filehandle to hold the log.
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...
auto max(const L &left, const R &right)
virtual Real computeDT() override
Called to compute _current_dt for a normal step.
short _direction
Multiplier specifying the direction the timestep is currently going.
SolutionTimeAdaptiveDT(const InputParameters &parameters)
virtual Real computeInitialDT() override
Called to compute _current_dt for the first timestep.
virtual void rejectStep() override
This gets called when time step is rejected.
auto log(const T &)
DIE A HORRIBLE DEATH HERE typedef LIBMESH_DEFAULT_SCALAR_TYPE Real
void max(const T &r, T &o, Request &req) const
virtual void rejectStep()
This gets called when time step is rejected.
Definition: TimeStepper.C:185
void mooseError(Args &&... args) const
Emits an error prefixed with object name and type.
virtual void step()
Take a time step.
Definition: TimeStepper.C:166
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 addParam(const std::string &name, const S &value, const std::string &doc_string)
These methods add an option parameter and a documentation string to the InputParameters object...
int & _t_step
Definition: TimeStepper.h:133
const ConsoleStream _console
An instance of helper class to write streams to the Console objects.
virtual void step() override
Take a time step.
processor_id_type processor_id() const
Real & _dt_max
Definition: TimeStepper.h:136
Real & _dt
Definition: TimeStepper.h:134
Real getCurrentDT()
Get the current_dt.
Definition: TimeStepper.h:85
void ErrorVector unsigned int
Real & _time
Values from executioner.
Definition: TimeStepper.h:131