diff --git a/CMakeLists.txt b/CMakeLists.txt index 2800f6030..fd0bbf457 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -413,6 +413,7 @@ add_library(libopenmc SHARED src/physics_mg.cpp src/plot.cpp src/position.cpp + src/progress_bar.cpp src/pugixml/pugixml_c.cpp src/random_lcg.cpp src/reaction.cpp diff --git a/include/openmc/progress_bar.h b/include/openmc/progress_bar.h new file mode 100644 index 000000000..18a3eea47 --- /dev/null +++ b/include/openmc/progress_bar.h @@ -0,0 +1,22 @@ +#ifndef OPENMC_PROGRESSBAR_H +#define OPENMC_PROGRESSBAR_H + +#include + +class ProgressBar { + +public: + // Constructor + ProgressBar(); + + void set_value(double val); + +private: + std::string bar; + char bar_old[72] = "???% | |"; + +}; + + +#endif // OPENMC_PROGRESSBAR_H + diff --git a/src/plot.cpp b/src/plot.cpp index 50bfc4917..eaa1de2b2 100644 --- a/src/plot.cpp +++ b/src/plot.cpp @@ -14,6 +14,7 @@ #include "openmc/hdf5_interface.h" #include "openmc/random_lcg.h" #include "openmc/output.h" +#include "openmc/progress_bar.h" namespace openmc { @@ -951,10 +952,13 @@ void create_voxel(Plot pl) int data[pl.pixels[1]][pl.pixels[2]]; + ProgressBar pb; + RGBColor rgb; int id; for (int x = 0; x < pl.pixels[0]; x++) { // TODO: progress bar here + pb.set_value(((double)x/(double)pl.pixels[0])*100); for (int y = 0; y < pl.pixels[1]; y++) { for (int z = 0; z < pl.pixels[2]; z++) { // get voxel color diff --git a/src/progress_bar.cpp b/src/progress_bar.cpp new file mode 100644 index 000000000..32fff626b --- /dev/null +++ b/src/progress_bar.cpp @@ -0,0 +1,52 @@ + +#include "openmc/progress_bar.h" + +#include +#include +#include + +#define BAR_WIDTH 72 + +ProgressBar::ProgressBar() { + // bar = "???% | |"; + + set_value(0.0); +} + +void +ProgressBar::set_value(double val) { + // set the bar percentage + if (val >= 100.0) { + bar.append("100"); + } else if (val <= 0.0) { + bar.append(" 0"); + } else { + std::stringstream ss; + ss << std::setfill(' ') << std::setw(3) << (int)val; + bar.append(ss.str()); + } + + bar.append("% |"); + + // remaining width of the bar + int remain = BAR_WIDTH - bar.size() - 1; + + // set the bar width + if (val >= 100.0) { + bar.append(remain, '='); + } else { + int width = (int)(65*val/100); + std::cout << "Setting width: " << width; + bar.append(width, '='); + bar.append(1, '>'); + bar.append(remain-width-1, ' '); + } + + bar.append("|"); + + // write the bar + std::cout << '\r' << bar << std::flush; + + // reset the bar value + bar = ""; //???% | |"; +} diff --git a/tests/regression_tests/plot/plots.xml b/tests/regression_tests/plot/plots.xml index ecfe69125..6651a5dfc 100644 --- a/tests/regression_tests/plot/plots.xml +++ b/tests/regression_tests/plot/plots.xml @@ -24,7 +24,7 @@ - 100 100 10 + 10000 100 10 0. 0. 0. 20 20 10