www.mooseframework.org
SolutionTimeAdaptiveDT.C
Go to the documentation of this file.
1 /****************************************************************/
2 /* DO NOT MODIFY THIS HEADER */
3 /* MOOSE - Multiphysics Object Oriented Simulation Environment */
4 /* */
5 /* (c) 2010 Battelle Energy Alliance, LLC */
6 /* ALL RIGHTS RESERVED */
7 /* */
8 /* Prepared by Battelle Energy Alliance, LLC */
9 /* Under Contract No. DE-AC07-05ID14517 */
10 /* With the U. S. Department of Energy */
11 /* */
12 /* See COPYRIGHT for full restrictions */
13 /****************************************************************/
14 
15 #include "SolutionTimeAdaptiveDT.h"
16 #include "FEProblem.h"
17 #include "Transient.h"
18 
19 #include <chrono>
20 
21 template <>
24 {
26  params.addParam<Real>(
27  "percent_change", 0.1, "Percentage to change the timestep by. Should be between 0 and 1");
28  params.addParam<int>(
29  "initial_direction", 1, "Direction for the first step. 1 for up... -1 for down. ");
30  params.addParam<bool>("adapt_log", false, "Output adaptive time step log");
31  params.addRequiredParam<Real>("dt", "The timestep size between solves");
32 
33  return params;
34 }
35 
37  : TimeStepper(parameters),
38  _direction(getParam<int>("initial_direction")),
39  _percent_change(getParam<Real>("percent_change")),
40  _older_sol_time_vs_dt(std::numeric_limits<Real>::max()),
41  _old_sol_time_vs_dt(std::numeric_limits<Real>::max()),
42  _sol_time_vs_dt(std::numeric_limits<Real>::max()),
43  _adapt_log(getParam<bool>("adapt_log"))
44 
45 {
46  if ((_adapt_log) && (processor_id() == 0))
47  {
48  _adaptive_log.open("adaptive_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 
70  _sol_time_vs_dt = elapsed_time / _dt;
71  }
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
89  _old_sol_time_vs_dt = std::numeric_limits<Real>::max();
90  _older_sol_time_vs_dt = std::numeric_limits<Real>::max();
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 
118 }
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.
InputParameters validParams< 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:31
The main MOOSE class responsible for handling user-defined parameters in almost every MOOSE system...
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...
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.
InputParameters validParams< TimeStepper >()
Definition: TimeStepper.C:22
virtual void rejectStep()
This gets called when time step is rejected.
Definition: TimeStepper.C:175
virtual void step()
Take a time step.
Definition: TimeStepper.C:158
virtual bool converged()
If the time step converged.
Definition: TimeStepper.C:182
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:131
const ConsoleStream _console
An instance of helper class to write streams to the Console objects.
virtual void step() override
Take a time step.
Real & _dt_max
Definition: TimeStepper.h:134
Real & _dt
Definition: TimeStepper.h:132
Real getCurrentDT()
Get the current_dt.
Definition: TimeStepper.h:89
Real & _time
Values from executioner.
Definition: TimeStepper.h:129