Skip to content

Commit

Permalink
new parameter to control how time to be formatted on screen idaholab#…
Browse files Browse the repository at this point in the history
  • Loading branch information
YaqiWang committed Jun 24, 2023
1 parent 27ef77a commit 4d41f2f
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 15 deletions.
11 changes: 11 additions & 0 deletions framework/include/outputs/Console.h
Expand Up @@ -87,6 +87,11 @@ class Console : public TableOutput
return _system_info_flags;
}

/**
* A reference to the time format to allow callers to set a new format for this console object
*/
MooseEnum & timeFormat() { return _time_format; }

protected:
/**
* Print the input file at the beginning of the simulation
Expand Down Expand Up @@ -150,6 +155,9 @@ class Console : public TableOutput
*/
void writeVariableNorms();

/// A help function to format a time
std::string formatTime(const Real t) const;

/// The max number of table rows
unsigned int _max_rows;

Expand Down Expand Up @@ -198,6 +206,9 @@ class Console : public TableOutput
/// Number of significant digits
unsigned int _precision;

/// Time format
MooseEnum _time_format;

private:
/**
* Add a message to the output streams
Expand Down
71 changes: 56 additions & 15 deletions framework/src/outputs/Console.C
Expand Up @@ -66,6 +66,11 @@ Console::validParams()
params.addParam<unsigned int>(
"time_precision",
"The number of significant digits that are printed on time related outputs");
MooseEnum time_format("plain=0 second=1 dtime=2", "plain");
params.addParam<MooseEnum>(
"time_format",
time_format,
"The format for the printed times ('dtime' means a format like 1d 01:01:0.1s)");

// Performance Logging
params.addDeprecatedParam<bool>("perf_log",
Expand Down Expand Up @@ -181,6 +186,7 @@ Console::Console(const InputParameters & parameters)
_outlier_variable_norms(getParam<bool>("outlier_variable_norms")),
_outlier_multiplier(getParam<std::vector<Real>>("outlier_multiplier")),
_precision(isParamValid("time_precision") ? getParam<unsigned int>("time_precision") : 0),
_time_format(getParam<MooseEnum>("time_format")),
_console_buffer(_app.getOutputWarehouse().consoleBuffer()),
_old_linear_norm(std::numeric_limits<Real>::max()),
_old_nonlinear_norm(std::numeric_limits<Real>::max()),
Expand Down Expand Up @@ -413,38 +419,30 @@ Console::writeTimestepInformation(bool output_dt)
// Write time step and time information
oss << "\nTime Step " << timeStep();

// Set precision
if (_precision > 0)
oss << std::setw(_precision) << std::setprecision(_precision) << std::setfill('0')
<< std::showpoint;

// Show scientific notation
if (_scientific_time)
oss << std::scientific;

// Print the time
oss << ", time = " << time();
oss << ", time = " << formatTime(time());

if (output_dt)
{
if (!_verbose)
// Show the time delta information
oss << ", dt = " << std::left << dt();
oss << ", dt = " << std::left << formatTime(dt());

// Show old time information, if desired on separate lines
else
{
oss << '\n'
<< std::right << std::setw(21) << std::setfill(' ') << "old time = " << std::left
<< timeOld() << '\n';
<< formatTime(timeOld()) << '\n';

// Show the time delta information
oss << std::right << std::setw(21) << std::setfill(' ') << "dt = " << std::left << dt()
<< '\n';
oss << std::right << std::setw(21) << std::setfill(' ') << "dt = " << std::left
<< formatTime(dt()) << '\n';

// Show the old time delta information, if desired
if (_verbose)
oss << std::right << std::setw(21) << std::setfill(' ') << "old dt = " << _dt_old << '\n';
oss << std::right << std::setw(21) << std::setfill(' ')
<< "old dt = " << formatTime(_dt_old) << '\n';
}
}

Expand All @@ -455,6 +453,49 @@ Console::writeTimestepInformation(bool output_dt)
}
}

std::string
Console::formatTime(const Real t) const
{
std::ostringstream oss;
if (_time_format == "plain" || _time_format == "second")
{
if (_precision > 0)
oss << std::setw(_precision) << std::setprecision(_precision) << std::setfill('0')
<< std::showpoint;
if (_scientific_time)
oss << std::scientific;
oss << t;
if (_time_format == "second")
oss << "s";
}
else if (_time_format == "dtime")
{
int days = std::floor(t / 24 / 3600);
int hours = std::floor(t / 3600 - days * 24);
int mins = std::floor(t / 60 - days * 24 * 60 - hours * 60);
Real second = t - days * 24 * 3600 - hours * 3600 - mins * 60;

if (days != 0)
oss << days << "d";
if (hours != 0 || mins != 0 || second != 0)
{
oss << " ";
oss << std::setfill('0') << std::setw(2) << hours << ":" << std::setfill('0') << std::setw(2)
<< mins << ":";

if (_precision > 0)
oss << std::setw(_precision) << std::setprecision(_precision) << std::setfill('0')
<< std::showpoint;
if (_scientific_time)
oss << std::scientific;
else if (second < 10)
oss << "0";
oss << second;
}
}
return oss.str();
}

void
Console::writeVariableNorms()
{
Expand Down

0 comments on commit 4d41f2f

Please sign in to comment.