36 |
|
* [1] Meineke, et al., J. Comp. Chem. 26, 252-271 (2005). |
37 |
|
* [2] Fennell & Gezelter, J. Chem. Phys. 124, 234104 (2006). |
38 |
|
* [3] Sun, Lin & Gezelter, J. Chem. Phys. 128, 24107 (2008). |
39 |
< |
* [4] Vardeman, Stocker & Gezelter, in progress (2010). |
39 |
> |
* [4] Kuang & Gezelter, J. Chem. Phys. 133, 164101 (2010). |
40 |
> |
* [5] Vardeman, Stocker & Gezelter, J. Chem. Theory Comput. 7, 834 (2011). |
41 |
|
*/ |
42 |
|
|
43 |
< |
#include <string> |
44 |
< |
#include <sstream> |
43 |
> |
#include <iostream> |
44 |
> |
#include <cstdlib> |
45 |
> |
#include <cstdio> |
46 |
|
#include <sys/ioctl.h> |
45 |
– |
#define _POSIX_SOURCE |
47 |
|
#include <unistd.h> |
48 |
|
#ifdef IS_MPI |
49 |
|
#include <mpi.h> |
50 |
< |
#endif //is_mpi |
50 |
> |
#endif |
51 |
|
#include "utils/ProgressBar.hpp" |
52 |
|
|
53 |
+ |
using namespace std; |
54 |
+ |
|
55 |
|
namespace OpenMD { |
56 |
|
|
57 |
|
const char * progressSpinner_ = "|/-\\"; |
63 |
|
#ifdef IS_MPI |
64 |
|
if (MPI::COMM_WORLD.Get_rank() == 0) { |
65 |
|
#endif |
66 |
< |
printf("\n"); |
67 |
< |
fflush(stdout); |
66 |
> |
cout << endl; |
67 |
> |
cout.flush(); |
68 |
|
#ifdef IS_MPI |
69 |
|
} |
70 |
|
#endif |
75 |
|
} |
76 |
|
|
77 |
|
void ProgressBar::update() { |
78 |
< |
int width = 80; |
78 |
> |
|
79 |
> |
int width; |
80 |
> |
|
81 |
|
#ifdef IS_MPI |
82 |
|
if (MPI::COMM_WORLD.Get_rank() == 0) { |
83 |
|
#endif |
84 |
< |
|
84 |
> |
|
85 |
|
// only do the progress bar if we are actually running in a tty: |
86 |
< |
if (isatty(fileno(stdout))) { |
86 |
> |
if (isatty(fileno(stdout)) && (getenv("SGE_TASK_ID")==NULL)) { |
87 |
|
// get the window width: |
88 |
|
struct winsize w; |
89 |
|
ioctl(fileno(stdout), TIOCGWINSZ, &w); |
90 |
|
width = w.ws_col; |
91 |
|
|
92 |
+ |
// handle the case when the width is returned as a nonsensical value. |
93 |
+ |
if (width <= 0) width = 80; |
94 |
+ |
|
95 |
|
// We'll use: |
96 |
|
// 31 characters for the completion estimate, |
97 |
|
// 6 for the % complete, |
98 |
|
// 2 characters for the open and closing brackets. |
99 |
|
|
100 |
< |
int avail = width - 31 - 6 - 2; |
101 |
< |
|
100 |
> |
int avail = width - 31 - 6 - 2; |
101 |
> |
|
102 |
|
++iteration_; |
103 |
< |
|
103 |
> |
|
104 |
|
if (maximum_ > 0.0) { |
105 |
|
|
106 |
|
// we know the maximum, so draw a progress bar |
107 |
|
|
108 |
|
RealType percent = value_ * 100.0 / maximum_; |
109 |
|
int hashes = int(percent * avail / 100.0); |
102 |
– |
std::string progressbar; |
103 |
– |
progressbar.assign(hashes, '#'); |
110 |
|
|
105 |
– |
// add the spinner to the end of the progress bar: |
106 |
– |
progressbar += progressSpinner_[iteration_ & 3]; |
107 |
– |
|
111 |
|
// compute the best estimate of the ending time: |
112 |
|
time_t current_ = time(NULL); |
113 |
|
time_t end_ = start_ + (current_ - start_) * (100.0/percent); |
114 |
|
struct tm * ender = localtime(&end_); |
115 |
< |
char buffer[24]; |
116 |
< |
strftime(buffer, 24, "%a %b %d @ %I:%M %p", ender); |
115 |
> |
char buffer[22]; |
116 |
> |
strftime(buffer, 22, "%a %b %d @ %I:%M %p", ender); |
117 |
|
|
118 |
< |
std::stringstream fmt; |
119 |
< |
fmt << "\r%3d%% [%-" << avail << "s] Estimate: %s"; |
120 |
< |
std::string st = fmt.str(); |
121 |
< |
|
122 |
< |
printf(st.c_str(), int(percent), |
123 |
< |
progressbar.c_str(), |
124 |
< |
buffer); |
125 |
< |
|
126 |
< |
} else { |
127 |
< |
// we don't know the maximum, so we can't draw a progress bar |
128 |
< |
int center = (iteration_ % 48) + 1; // 50 spaces, minus 2 |
129 |
< |
std::string before; |
130 |
< |
std::string after; |
131 |
< |
before.assign(std::max(center - 2, 0), ' '); |
132 |
< |
after.assign(std::min(center + 2, 50), ' '); |
133 |
< |
|
134 |
< |
printf("\r[%s###%s] ", |
135 |
< |
before.c_str(), after.c_str()); |
118 |
> |
cout << '\r'; |
119 |
> |
cout.width(3); |
120 |
> |
cout << right << int(percent); |
121 |
> |
cout.width(3); |
122 |
> |
cout << "% ["; |
123 |
> |
cout.fill('#'); |
124 |
> |
if (hashes+1 < avail) { |
125 |
> |
cout.width(hashes+1); |
126 |
> |
cout << progressSpinner_[iteration_ & 3]; |
127 |
> |
} else { |
128 |
> |
cout.width(avail); |
129 |
> |
cout << '#'; |
130 |
> |
} |
131 |
> |
cout.fill(' '); |
132 |
> |
if (avail - hashes - 1 > 0) { |
133 |
> |
cout.width(avail - hashes - 1); |
134 |
> |
cout << ' '; |
135 |
> |
} |
136 |
> |
cout.width(11); |
137 |
> |
cout << "] Estimate:"; |
138 |
> |
cout.width(22); |
139 |
> |
cout << buffer; |
140 |
> |
|
141 |
|
} |
142 |
< |
fflush(stdout); |
142 |
> |
cout.flush(); |
143 |
|
} |
144 |
|
#ifdef IS_MPI |
145 |
|
} |