ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/OpenMD/branches/development/src/utils/ProgressBar.cpp
(Generate patch)

Comparing:
trunk/src/utils/ProgressBar.cpp (file contents), Revision 1413 by gezelter, Mon Mar 22 19:21:22 2010 UTC vs.
branches/development/src/utils/ProgressBar.cpp (file contents), Revision 1665 by gezelter, Tue Nov 22 20:38:56 2011 UTC

# Line 36 | Line 36
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>
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_ = "|/-\\";
# Line 58 | Line 63 | namespace OpenMD {
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
# Line 70 | Line 75 | namespace OpenMD {
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
77 #ifndef IS_MPI
78    // get the window width:
79    struct ttysize ts;
80    ioctl(0, TIOCGWINSZ, &ts);
81    width = ts.ts_cols;
82 #endif  
83
84    // We'll use:
85    // 31 characters for the completion estimate,
86    //  6 for the % complete,
87    //  2 characters for the open and closing brackets.
88
89    int avail = width - 31 - 6 - 2;
90
91    ++iteration_;
92    
93    if (maximum_ > 0.0) {
94      // we know the maximum, so
95      // draw a progress bar
84        
85 <      RealType percent = value_ * 100.0 / maximum_;
86 <      int hashes = int(percent * avail / 100.0);
87 <      std::string progressbar;
88 <      progressbar.assign(hashes, '#');
85 >      // only do the progress bar if we are actually running in a tty:
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 <      // add the spinner to the end of the progress bar:
93 <      progressbar += progressSpinner_[iteration_ & 3];
92 >        // handle the case when the width is returned as a nonsensical value.
93 >        if (width <= 0) width = 80;
94  
95 <      // compute the best estimate of the ending time:
96 <      time_t current_ = time(NULL);
97 <      time_t end_ = start_ + (current_ - start_) * (100.0/percent);
98 <      struct tm * ender = localtime(&end_);
99 <      char buffer[24];
100 <      strftime(buffer, 24, "%a %b %d @ %I:%M %p", ender);
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  
102 <      std::stringstream fmt;
113 <      fmt << "\r%3d%% [%-" << avail << "s] Estimate: %s";
114 <      std::string st = fmt.str();
102 >        ++iteration_;
103  
104 <      printf(st.c_str(), int(percent),
117 <             progressbar.c_str(),
118 <             buffer);
104 >        if (maximum_ > 0.0) {
105  
106 <    } else {
107 <      // we don't know the maximum, so we can't draw a progress bar
108 <      int center = (iteration_ % 48) + 1; // 50 spaces, minus 2
109 <      std::string before;
110 <      std::string after;
111 <      before.assign(std::max(center - 2, 0), ' ');
112 <      after.assign(std::min(center + 2, 50), ' ');
113 <      
114 <      printf("\r[%s###%s]            ",
115 <             before.c_str(), after.c_str());
116 <    }
117 <    fflush(stdout);
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);
110 >          
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[22];
116 >          strftime(buffer, 22, "%a %b %d @ %I:%M %p", ender);
117 >          
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 >        cout.flush();
143 >      }
144   #ifdef IS_MPI
145      }
146   #endif

Comparing:
trunk/src/utils/ProgressBar.cpp (property svn:keywords), Revision 1413 by gezelter, Mon Mar 22 19:21:22 2010 UTC vs.
branches/development/src/utils/ProgressBar.cpp (property svn:keywords), Revision 1665 by gezelter, Tue Nov 22 20:38:56 2011 UTC

# Line 0 | Line 1
1 + Author Id Revision Date

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines