https://mooseframework.inl.gov
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 
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  auto solve_end = std::chrono::system_clock::now();
63  auto elapsed_time =
64  std::chrono::duration_cast<std::chrono::milliseconds>(solve_end - solve_start).count();
65 
66  // Take the maximum time over all processors so all processors compute and use the same dt
67  TimeStepper::_communicator.max(elapsed_time);
68 
71  _sol_time_vs_dt = elapsed_time / _dt;
72 }
73 
74 Real
76 {
77  return getParam<Real>("dt");
78 }
79 
80 Real
82 {
83  // Ratio grew so switch direction
85  {
86  _direction *= -1;
87 
88  // Make sure we take at least two steps in this new direction
91  }
92 
93  Real local_dt = _dt + _dt * _percent_change * _direction;
94 
95  if ((_adapt_log) && (processor_id() == 0))
96  {
97  Real out_dt = getCurrentDT();
98  if (out_dt > _dt_max)
99  out_dt = _dt_max;
100 
101  _adaptive_log << "***Time step: " << _t_step << ", time = " << _time + out_dt
102  << "\nCur DT: " << out_dt << "\nOlder Ratio: " << _older_sol_time_vs_dt
103  << "\nOld Ratio: " << _old_sol_time_vs_dt << "\nNew Ratio: " << _sol_time_vs_dt
104  << std::endl;
105  }
106 
107  return local_dt;
108 }
109 
110 void
112 {
113  _console << "Solve failed... cutting timestep" << std::endl;
114  if (_adapt_log)
115  _adaptive_log << "Solve failed... cutting timestep" << std::endl;
116 
117  // Reverse progression of quantities
120 
122 }
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
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
Computes time step size after the initial time step.
short _direction
Multiplier specifying the direction the timestep is currently going.
SolutionTimeAdaptiveDT(const InputParameters &parameters)
virtual Real computeInitialDT() override
Computes time step size for the initial time step.
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 optional parameter and a documentation string to the InputParameters object...
int & _t_step
Definition: TimeStepper.h:127
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:130
Real & _dt
Definition: TimeStepper.h:128
Real getCurrentDT()
Get the current_dt.
Definition: TimeStepper.h:85
void ErrorVector unsigned int
Real & _time
Values from executioner.
Definition: TimeStepper.h:125