deltaFlow
Progress.H
Go to the documentation of this file.
1/*
2 * Copyright (c) 2024 Saud Zahir
3 *
4 * This file is part of deltaFlow.
5 *
6 * deltaFlow is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public
8 * License as published by the Free Software Foundation; either
9 * version 3 of the License, or (at your option) any later version.
10 *
11 * deltaFlow is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public
17 * License along with deltaFlow. If not, see
18 * <https://www.gnu.org/licenses/>.
19 */
20
29#ifndef PROGRESS_BAR_H
30#define PROGRESS_BAR_H
31
32#include <fmt/color.h>
33#include <fmt/core.h>
34#include <string>
35
51 const std::string& solver,
52 int iter,
53 int maxIter,
54 double error,
55 double tol,
56 int barWidth = 50
57) {
58 float ratio = maxIter == 0 ? 0.0f : static_cast<float>(iter) / maxIter;
59 int filled = static_cast<int>(ratio * barWidth);
60
61 // Move cursor up to overwrite previous line (except on first iteration)
62 if (iter > 1)
63 fmt::print("\033[1F\033[2K");
64
65 fmt::print(fg(fmt::color::cyan) | fmt::emphasis::bold, "{:<16}", solver);
66
67 // Progress bar
68 for (int i = 0; i < filled; ++i)
69 fmt::print(bg(fmt::color::green), " ");
70 for (int i = filled; i < barWidth; ++i)
71 fmt::print(bg(fmt::color::dark_gray), " ");
72
73 // Reset colors
74 fmt::print("\033[0m");
75
76 // Stats
77 fmt::print(" iter ");
78 fmt::print(fg(fmt::color::yellow) | fmt::emphasis::bold, "{:>4}", iter);
79 fmt::print(fg(fmt::color::white), "/{:<4}", maxIter);
80 fmt::print(" error ");
81 fmt::print(fg(fmt::color::yellow), "{:.6e}", error);
82 fmt::print(fg(fmt::color::white), " tol {:.0e}\n", tol);
83}
84
101 const std::string& solver,
102 bool converged,
103 int iter,
104 int maxIter,
105 double error,
106 double tol,
107 int barWidth = 50
108) {
109 // Overwrite the last progress line
110 fmt::print("\033[1F\033[2K");
111
112 float ratio = maxIter == 0 ? 0.0f : static_cast<float>(iter) / maxIter;
113 int filled = static_cast<int>(ratio * barWidth);
114
115 fmt::print(fg(fmt::color::cyan) | fmt::emphasis::bold, "{:<16}", solver);
116
117 if (converged) {
118 // Green bar up to convergence point, gray for the rest
119 for (int i = 0; i < filled; ++i)
120 fmt::print(bg(fmt::color::green), " ");
121 for (int i = filled; i < barWidth; ++i)
122 fmt::print(bg(fmt::color::dark_gray), " ");
123 fmt::print("\033[0m");
124
125 fmt::print(" ");
126 fmt::print(fg(fmt::color::green) | fmt::emphasis::bold, "CONVERGED");
127 fmt::print(fg(fmt::color::white), " in ");
128 fmt::print(fg(fmt::color::yellow) | fmt::emphasis::bold, "{}", iter);
129 fmt::print(fg(fmt::color::white), " iterations");
130 fmt::print(fg(fmt::color::white), " error {:.6e}\n", error);
131 } else {
132 // Red bar up to failure point, dark gray for the rest
133 for (int i = 0; i < filled; ++i)
134 fmt::print(bg(fmt::color::red), " ");
135 for (int i = filled; i < barWidth; ++i)
136 fmt::print(bg(fmt::color::dark_gray), " ");
137 fmt::print("\033[0m");
138
139 fmt::print(" ");
140 fmt::print(fg(fmt::color::red) | fmt::emphasis::bold, "FAILED");
141 fmt::print(fg(fmt::color::white), " after ");
142 fmt::print(fg(fmt::color::yellow) | fmt::emphasis::bold, "{}", iter);
143 fmt::print(fg(fmt::color::white), " iterations");
144 fmt::print(fg(fmt::color::white), " error {:.6e}\n", error);
145 }
146}
147
148#endif
void printIterationProgress(const std::string &solver, int iter, int maxIter, double error, double tol, int barWidth=50)
Print an iteration progress line for a solver.
Definition Progress.H:50
void printConvergenceStatus(const std::string &solver, bool converged, int iter, int maxIter, double error, double tol, int barWidth=50)
Print the final convergence status line.
Definition Progress.H:100