Skip to content

Commit

Permalink
Only print mid-timestep estTimeStep outputs if relevant (#3023)
Browse files Browse the repository at this point in the history
Output from estTimeStep is occurring for the pre- and post-timestep validity checks, but on most timesteps this clutters the output because the validity check passes. This PR creates a class that temporarily redirects std::cout and then uses that to capture the estTimeStep output and only print it if needed.
  • Loading branch information
maxpkatz authored Jan 20, 2025
1 parent d6711fa commit f7c5311
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 2 deletions.
1 change: 1 addition & 0 deletions Source/driver/Castro.H
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
// runtime parameters
#include <castro_params.H>
#include <prob_parameters.H>
#include <Castro_io.H>
#include <Castro_util.H>

using namespace castro;
Expand Down
26 changes: 24 additions & 2 deletions Source/driver/Castro_advance.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -229,10 +229,21 @@ Castro::initialize_do_advance (Real time, Real dt)

if (castro::check_dt_before_advance && !is_first_step_on_this_level) {
int is_new = 0;
Real old_dt = estTimeStep(is_new);

// We only display the estTimeStep output if the validity check fails
std::string estTimeStep_output;

Real old_dt;

{
CoutRedirection redirection;
old_dt = estTimeStep(is_new);
estTimeStep_output = redirection.getCapturedOutput();
}

if (castro::change_max * old_dt < dt) {
status.success = false;
std::cout << estTimeStep_output;
status.reason = "pre-advance timestep validity check failed";
}
}
Expand Down Expand Up @@ -276,10 +287,21 @@ Castro::finalize_do_advance (Real time, Real dt)

if (do_validity_check) {
int is_new = 1;
Real new_dt = estTimeStep(is_new);

// We only display the estTimeStep output if the validity check fails
std::string estTimeStep_output;

Real new_dt;

{
CoutRedirection redirection;
new_dt = estTimeStep(is_new);
estTimeStep_output = redirection.getCapturedOutput();
}

if (castro::change_max * new_dt < dt) {
status.success = false;
std::cout << estTimeStep_output;
status.reason = "post-advance timestep validity check failed";
return status;
}
Expand Down
32 changes: 32 additions & 0 deletions Source/driver/Castro_io.H
Original file line number Diff line number Diff line change
@@ -1,6 +1,38 @@
#ifndef CASTRO_IO_H
#define CASTRO_IO_H

#include <iostream>
#include <sstream>

extern std::string inputs_name;

// Redirect std::cout to a temporary buffer.

class CoutRedirection {

public:
CoutRedirection () : original_output(std::cout.rdbuf()) {
std::cout.rdbuf(captured_output.rdbuf());
}

~CoutRedirection () {
std::cout.rdbuf(original_output);
}

// Remove copy/move constructors/assignment operators.
CoutRedirection (const CoutRedirection&) = delete;
CoutRedirection (CoutRedirection&&) = delete;
CoutRedirection& operator= (const CoutRedirection&) = delete;
CoutRedirection& operator= (CoutRedirection&&) = delete;

std::string getCapturedOutput () {
return captured_output.str();
}

private:
std::ostringstream captured_output;
std::streambuf* original_output;

};

#endif

0 comments on commit f7c5311

Please sign in to comment.