Skip to content

Commit

Permalink
Make cosmetics consistent
Browse files Browse the repository at this point in the history
  • Loading branch information
davidscn committed Oct 3, 2023
1 parent 66f7db7 commit c0e0109
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 28 deletions.
37 changes: 36 additions & 1 deletion include/base/utilities.h
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,42 @@ namespace Utilities
<< std::endl;
}


/**
* @brief Helper function to convert different representations of the same number
* (in double rpecision) to a common format by truncating trailing zeros.
*
* Examples:
* number = 0.0100 -> 0.01
* number = 42.0100 -> 42.01
* number = 42.00 -> 42
*
*
* @param number the number to format
* @return std::string the string representation
*/
std::string
format_time_stamp_to_string(double number)
{
// We need to apply some cosmetics to t to make it usable
std::string str = std::to_string(number);
for (int i = str.size() - 1; i >= 1; i--)
{
if (str.at(i) == '0')
{
str.pop_back(); // Remove if last digit is '0'.
}
else if (str.at(i) == '.')
{
str.pop_back(); // Remove dot.
break; // Break after '.' is removed.
}
else
{
break; // Or break before a digit is removed.
}
}
return str;
}

/**
* @brief Rounds a given number up to a defined precision.
Expand Down
56 changes: 29 additions & 27 deletions include/solid_mechanics/mf_elasticity.h
Original file line number Diff line number Diff line change
Expand Up @@ -564,7 +564,8 @@ namespace FSI
testcase = testcase_;
make_grid();
system_setup();
output_results(0);
if (parameters.start_time != 0)
output_results(0);
time.increment();

// We then declare the incremental solution update $\varDelta
Expand Down Expand Up @@ -623,9 +624,12 @@ namespace FSI
time.increment();
}
}
// has to come after we incremented the time
// write the restart after we finished
// we decrement the time in the write_restart function, as we call the
// function after we incremented the time for the next iteration
write_restart();


// for post-processing, print average CG iterations over the whole run:
timer_out << std::endl
<< "Average CG iter = "
Expand All @@ -648,8 +652,14 @@ namespace FSI
testcase->make_coarse_grid_and_bcs(triangulation);

if (parameters.start_time != 0)
triangulation.load(parameters.output_folder + "restart-t-" +
std::to_string(parameters.start_time) + ".mesh");
{
std::string restart_file(
parameters.output_folder + "restart-t-" +
Utilities::format_time_stamp_to_string(parameters.start_time) +
".mesh");
pcout << "-- Looking for restart file \"" << restart_file << "\"\n";
triangulation.load(restart_file);
}
else
triangulation.refine_global(parameters.n_global_refinement);

Expand Down Expand Up @@ -2063,29 +2073,18 @@ namespace FSI
void
Solid<dim, Number>::write_restart()
{
// We need to apply some cosmetics to t to make it usable
std::string t_string = std::to_string(time.current());
for (int i = t_string.size() - 1; i >= 1; i--)
{
if (t_string.at(i) == '0')
{
t_string.pop_back(); // Remove if last digit is '0'.
}
else if (t_string.at(i) == '.')
{
t_string.pop_back(); // Remove dot.
break; // Break after '.' is removed.
}
else
{
break; // Or break before a digit is removed.
}
}
// first, decrement the time, as we call the function after an (unnecessary)
// increment
double t_decrement = time.current() - time.get_delta_t();
unsigned int timestep_decrement = time.get_timestep() - 1;

auto t_string = Utilities::format_time_stamp_to_string(t_decrement);

std::string restart_file(parameters.output_folder + "restart-t-" +
t_string);
pcout << "-- Creating restart files \"" + restart_file +
"\" for t = " + t_string
<< " ( timestep " << time.get_timestep() << " ) "
<< " ( timestep " << timestep_decrement << " ) "
<< "\n";
std::vector<const VectorType *> in_vectors({&total_displacement,
&velocity,
Expand All @@ -2104,8 +2103,8 @@ namespace FSI
total_displacement.get_partitioner(),
in_vectors,
restart_file,
time.current(),
time.get_timestep());
t_decrement,
timestep_decrement);
}


Expand All @@ -2121,8 +2120,9 @@ namespace FSI
&velocity_old,
&acceleration_old});

std::string restart_file(parameters.output_folder + "restart-t-" +
std::to_string(parameters.start_time));
std::string restart_file(
parameters.output_folder + "restart-t-" +
Utilities::format_time_stamp_to_string(parameters.start_time));

double loaded_time = 0;
unsigned int loaded_timestep = 0;
Expand All @@ -2134,6 +2134,8 @@ namespace FSI
<< loaded_time << " ( timestep " << loaded_timestep << " ) "
<< "\n";
time.set_time(loaded_time, loaded_timestep);
// we loaded the time we already computed, so let's move on to the next step
time.increment();
}


Expand Down

0 comments on commit c0e0109

Please sign in to comment.