OpenMD 3.0
Molecular Dynamics in the Open
Loading...
Searching...
No Matches
ProgressBar.cpp
1/*
2 * Copyright (c) 2004-present, The University of Notre Dame. All rights
3 * reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
7 *
8 * 1. Redistributions of source code must retain the above copyright notice,
9 * this list of conditions and the following disclaimer.
10 *
11 * 2. Redistributions in binary form must reproduce the above copyright notice,
12 * this list of conditions and the following disclaimer in the documentation
13 * and/or other materials provided with the distribution.
14 *
15 * 3. Neither the name of the copyright holder nor the names of its
16 * contributors may be used to endorse or promote products derived from
17 * this software without specific prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
20 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
23 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
30 *
31 * SUPPORT OPEN SCIENCE! If you use OpenMD or its source code in your
32 * research, please cite the appropriate papers when you publish your
33 * work. Good starting points are:
34 *
35 * [1] Meineke, et al., J. Comp. Chem. 26, 252-271 (2005).
36 * [2] Fennell & Gezelter, J. Chem. Phys. 124, 234104 (2006).
37 * [3] Sun, Lin & Gezelter, J. Chem. Phys. 128, 234107 (2008).
38 * [4] Vardeman, Stocker & Gezelter, J. Chem. Theory Comput. 7, 834 (2011).
39 * [5] Kuang & Gezelter, Mol. Phys., 110, 691-701 (2012).
40 * [6] Lamichhane, Gezelter & Newman, J. Chem. Phys. 141, 134109 (2014).
41 * [7] Lamichhane, Newman & Gezelter, J. Chem. Phys. 141, 134110 (2014).
42 * [8] Bhattarai, Newman & Gezelter, Phys. Rev. B 99, 094106 (2019).
43 */
44
45#include <cstdlib>
46#include <iostream>
47
48#ifdef _MSC_VER
49#include <Windows.h>
50#include <io.h>
51#include <stdio.h>
52#define isatty _isatty
53#define fileno _fileno
54#else
55#include <unistd.h>
56
57#include <cstdio>
58
59#include <sys/ioctl.h>
60#endif
61
62#ifdef IS_MPI
63#include <mpi.h>
64#endif
65
66#include "utils/ProgressBar.hpp"
67
68using namespace std;
69
70namespace OpenMD {
71
72 const char* progressSpinner_ = "|/-\\";
73
74 ProgressBar::ProgressBar() :
75 value_(0.0), maximum_(-1.0), iteration_(0), start_(time(NULL)) {}
76
77 void ProgressBar::clear() {
78#ifdef IS_MPI
79 int myRank;
80 MPI_Comm_rank(MPI_COMM_WORLD, &myRank);
81 if (myRank == 0) {
82#endif
83 cout << endl;
84 cout.flush();
85#ifdef IS_MPI
86 }
87#endif
88 iteration_ = 0;
89 value_ = 0;
90 maximum_ = -1;
91 start_ = time(NULL);
92 }
93
94 void ProgressBar::update() {
95#ifdef IS_MPI
96 int myRank;
97 MPI_Comm_rank(MPI_COMM_WORLD, &myRank);
98 if (myRank == 0) {
99#endif
100
101 // only do the progress bar if we are actually running in a tty:
102 if (isatty(fileno(stdout)) && (getenv("SGE_TASK_ID") == NULL)) {
103 // get the window width:
104
105 int width = 0;
106#ifdef _MSC_VER
107 CONSOLE_SCREEN_BUFFER_INFO csbi;
108 HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
109 int ret = GetConsoleScreenBufferInfo(hConsole, &csbi);
110 if (ret) { width = csbi.dwSize.X - 1; }
111#else
112 struct winsize w;
113 ioctl(fileno(stdout), TIOCGWINSZ, &w);
114 width = w.ws_col;
115#endif
116
117 // handle the case when the width is returned as a nonsensical value.
118 if (width <= 0) width = 80;
119
120 // We'll use:
121 // 31 characters for the completion estimate,
122 // 6 for the % complete,
123 // 2 characters for the open and closing brackets.
124
125 int avail = width - 31 - 6 - 2;
126
127 ++iteration_;
128
129 if (maximum_ > 0.0) {
130 // we know the maximum, so draw a progress bar
131
132 RealType percent =
133 std::min(std::max(value_ * 100.0 / maximum_, 1e-6), 100.0);
134
135 int hashes = int(percent * avail / 100.0);
136
137 // compute the best estimate of the ending time:
138 time_t current_ = time(NULL);
139 time_t end_ = static_cast<time_t>(start_ + (current_ - start_) *
140 (100.0 / percent));
141 struct tm* ender = localtime(&end_);
142 char buffer[22];
143 strftime(buffer, 22, "%a %b %d @ %I:%M %p", ender);
144
145#ifdef _MSC_VER
146 csbi.dwCursorPosition.X = 0;
147 SetConsoleCursorPosition(hConsole, csbi.dwCursorPosition);
148#else
149 cout << '\r';
150#endif
151 cout.width(3);
152 cout << right << int(percent);
153 cout.width(3);
154 cout << "% [";
155 cout.fill('#');
156 if (hashes + 1 < avail) {
157 cout.width(hashes + 1);
158 cout << progressSpinner_[iteration_ & 3];
159 } else {
160 cout.width(avail);
161 cout << '#';
162 }
163 cout.fill(' ');
164 if (avail - hashes - 1 > 0) {
165 cout.width(avail - hashes - 1);
166 cout << ' ';
167 }
168 cout.width(11);
169 cout << "] Estimate:";
170 cout.width(22);
171 cout << buffer;
172 }
173 cout.flush();
174 }
175#ifdef IS_MPI
176 }
177#endif
178 }
179
180 void ProgressBar::setStatus(RealType val, RealType max) {
181 value_ = val;
182 maximum_ = max;
183 }
184} // namespace OpenMD
This basic Periodic Table class was originally taken from the data.cpp file in OpenBabel.