Skip to content

Commit

Permalink
Fix CG net and structure files for new changes, fix progress bar update
Browse files Browse the repository at this point in the history
  • Loading branch information
peterspackman committed Jan 6, 2025
1 parent 763b4f3 commit fccd0cf
Show file tree
Hide file tree
Showing 8 changed files with 138 additions and 171 deletions.
8 changes: 5 additions & 3 deletions include/occ/core/progress.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#pragma once
#include <chrono>
#include <vector>
#include <string>
#include <vector>

namespace occ::core {

Expand All @@ -16,7 +16,7 @@ class ProgressTracker {
ProgressTracker(int total);

void set_tty(bool);
void update(int progress, int total, const std::string description= "");
void update(int progress, int total, const std::string description = "");
void reset();

void clear();
Expand All @@ -34,6 +34,8 @@ class ProgressTracker {
int m_total{0};
int m_current{0};
int m_window{5};
int m_current_progress{0};
bool m_started{false};
std::vector<TimePoint> m_time_points;
std::string m_name{"Progress"};
std::chrono::duration<double> m_average_time;
Expand All @@ -42,4 +44,4 @@ class ProgressTracker {
bool m_is_tty{false};
};

}
} // namespace occ::core
16 changes: 10 additions & 6 deletions include/occ/crystal/dimer_labeller.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,19 @@ struct SymmetryDimerLabeller {

std::string operator()(const occ::core::Dimer &dimer) const;

std::string connection{"<=>"};
SymmetryOperationFormat format{
"{}", // fmt string
"," // delimiter
};

private:
static std::string
format_symop_with_translation(const SymmetryOperation &symop,
const IVec3 &translation);
static std::string
std::string format_symop_with_translation(const SymmetryOperation &symop,
const IVec3 &translation) const;
std::string
format_molecule_part(const std::string &name, const SymmetryOperation &symop,
const IVec3 &translation = IVec3::Zero());
const IVec3 &translation = IVec3::Zero()) const;

std::string connection{"<=>"};
const Crystal &m_crystal;
};

Expand Down
3 changes: 2 additions & 1 deletion src/cg/interaction_mapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -159,8 +159,9 @@ void InteractionMapper::log_neighbor_info(size_t mol_idx,

void InteractionMapper::log_dimer_info(size_t neighbor_idx,
const Dimer &dimer) const {
auto shift_b = dimer.b().cell_shift();
const auto& uc_mols = m_crystal.unit_cell_molecules();
auto idx_b = dimer.b().unit_cell_molecule_idx();
auto shift_b = dimer.b().cell_shift() - uc_mols[idx_b].cell_shift();
double rc = dimer.centroid_distance();

occ::log::debug("{:<7d} {:>7d} {:>10s} {:7.2f} {:7.3f}", neighbor_idx, idx_b,
Expand Down
175 changes: 91 additions & 84 deletions src/core/progress.cpp
Original file line number Diff line number Diff line change
@@ -1,116 +1,120 @@
#include <occ/core/progress.h>
#include <occ/core/util.h>


#include <iostream>
#include <string>
#include <thread>
#include <chrono>
#include <occ/core/log.h>
#include <fmt/core.h>
#include <fmt/chrono.h>

#include <fmt/core.h>
#include <iostream>
#include <occ/core/log.h>
#include <string>

#if defined(_WIN32) || defined(_WIN64)
#include <windows.h>
#include <io.h>
#include <windows.h>
inline COORD occ_saved_cursor_position;
#define ISATTY _isatty
#define FILENO _fileno
#else
#include <unistd.h>
#include <sys/ioctl.h>
#include <unistd.h>
#define ISATTY isatty
#define FILENO fileno
#endif



namespace occ::core {

inline bool stdout_is_tty() {
return ISATTY(FILENO(stdout));
}
inline bool stdout_is_tty() { return ISATTY(FILENO(stdout)); }

#undef ISATTY
#undef FILENO

TerminalSize TerminalSize::get_current_size() {

#if defined(_WIN32) || defined(_WIN64)
HANDLE hStdOut = GetStdHandle(STD_OUTPUT_HANDLE);
CONSOLE_SCREEN_BUFFER_INFO csbi;
GetConsoleScreenBufferInfo(hStdOut, &csbi);
return TerminalSize{csbi.dwSize.X, csbi.dwSize.Y};
#else
struct winsize size;
ioctl(STDOUT_FILENO, TIOCGWINSZ, &size);
return TerminalSize{size.ws_row, size.ws_col};
HANDLE hStdOut = GetStdHandle(STD_OUTPUT_HANDLE);
CONSOLE_SCREEN_BUFFER_INFO csbi;
GetConsoleScreenBufferInfo(hStdOut, &csbi);
return TerminalSize{csbi.dwSize.X, csbi.dwSize.Y};
#else
struct winsize size;
ioctl(STDOUT_FILENO, TIOCGWINSZ, &size);
return TerminalSize{size.ws_row, size.ws_col};
#endif

}


ProgressTracker::ProgressTracker(int total) : m_total(total) {
m_is_tty = stdout_is_tty();
m_tsize = TerminalSize::get_current_size();
m_tsize = TerminalSize::get_current_size();
m_time_points.push_back(std::chrono::high_resolution_clock::now());
}



void ProgressTracker::save_cursor() {
#if defined(_WIN32) || defined(_WIN64)
HANDLE hStdOut = GetStdHandle(STD_OUTPUT_HANDLE);
CONSOLE_SCREEN_BUFFER_INFO csbi;
GetConsoleScreenBufferInfo(hStdOut, &csbi);
occ_saved_cursor_position = csbi.dwCursorPosition;
HANDLE hStdOut = GetStdHandle(STD_OUTPUT_HANDLE);
CONSOLE_SCREEN_BUFFER_INFO csbi;
GetConsoleScreenBufferInfo(hStdOut, &csbi);
occ_saved_cursor_position = csbi.dwCursorPosition;
#else
fmt::print("\033[s");
fmt::print("\033[s");
#endif
}

void ProgressTracker::restore_cursor() {
#if defined(_WIN32) || defined(_WIN64)
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), occ_saved_cursor_position);
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE),
occ_saved_cursor_position);
#else
fmt::print("\033[u"); // Restore cursor position
fmt::print("\033[u"); // Restore cursor position
#endif
}

void ProgressTracker::move_cursor_to_bottom() {
#if defined(_WIN32) || defined(_WIN64)
HANDLE hStdOut = GetStdHandle(STD_OUTPUT_HANDLE);
CONSOLE_SCREEN_BUFFER_INFO csbi;
GetConsoleScreenBufferInfo(hStdOut, &csbi);

COORD bottomCoord;
bottomCoord.X = 0;
bottomCoord.Y = m_tsize.rows;
SetConsoleCursorPosition(hStdOut, bottomCoord);
#else
fmt::print("\033[{};1H", m_tsize.rows);
HANDLE hStdOut = GetStdHandle(STD_OUTPUT_HANDLE);
CONSOLE_SCREEN_BUFFER_INFO csbi;
GetConsoleScreenBufferInfo(hStdOut, &csbi);

COORD bottomCoord;
bottomCoord.X = 0;
bottomCoord.Y = m_tsize.rows;
SetConsoleCursorPosition(hStdOut, bottomCoord);
#else
fmt::print("\033[{};1H", m_tsize.rows);
#endif
}

void ProgressTracker::update(int progress, int total,
const std::string description) {
if (!m_started) {
m_started = true;
m_time_points.push_back(std::chrono::high_resolution_clock::now());
}

void ProgressTracker::update(int progress, int total, const std::string description) {
if (progress != m_current_progress) {
m_time_points.push_back(std::chrono::high_resolution_clock::now());
m_current_progress = progress;
}

m_time_points.push_back(std::chrono::high_resolution_clock::now());
estimate_time_remaining();

std::string eta_string = "";
if(m_time_points.size() > 1) {
eta_string = occ::util::human_readable_time(std::chrono::duration_cast<std::chrono::milliseconds>(m_estimated_time_remaining));
std::string eta_string;
if (m_time_points.size() > 1) {
eta_string = occ::util::human_readable_time(
std::chrono::duration_cast<std::chrono::milliseconds>(
m_estimated_time_remaining));
}

float percent = static_cast<float>(progress) / total;
if(!m_is_tty) {
occ::log::info("{: <40s} {}/{} {: >3d}% {}", description, progress, total, static_cast<int>(percent * 100.0), eta_string);
if (!m_is_tty) {
occ::log::info("{: <40s} {}/{} {: >3d}% {}", description, progress, total,
static_cast<int>(percent * 100.0), eta_string);
return;
}

std::string left = fmt::format("{: <40s} {}/{} [", description, progress, total);
std::string right = fmt::format("| {: >3d}% {}", static_cast<int>(percent * 100.0), eta_string);
std::string left =
fmt::format("{: <40s} {}/{} [", description, progress, total);
std::string right = fmt::format(
"| {: >3d}% {}", static_cast<int>(percent * 100.0), eta_string);

const int bar_width = m_tsize.cols - left.size() - right.size();
int pos = bar_width * percent;
Expand All @@ -120,52 +124,55 @@ void ProgressTracker::update(int progress, int total, const std::string descript

fmt::print("{}", left);
for (int i = 0; i < bar_width; ++i) {
if (i <= pos) fmt::print("#");
else fmt::print(".");
if (i <= pos)
fmt::print("#");
else
fmt::print(".");
}
fmt::print("{}\r", right);
std::cout.flush();
restore_cursor();
}

void ProgressTracker::clear_progress_line() {
if(!m_is_tty) return;
save_cursor();
move_cursor_to_bottom();
for (int i = 0; i < m_tsize.cols; ++i) {
fmt::print(" ");
}
restore_cursor();
if (!m_is_tty)
return;
save_cursor();
move_cursor_to_bottom();
for (int i = 0; i < m_tsize.cols; ++i) {
fmt::print(" ");
}
restore_cursor();
}

void ProgressTracker::clear() {
clear_progress_line();
}
void ProgressTracker::clear() { clear_progress_line(); }

void ProgressTracker::set_tty(bool value) {
m_is_tty = value;
}
void ProgressTracker::set_tty(bool value) { m_is_tty = value; }

void ProgressTracker::estimate_time_remaining() {
int completed = m_time_points.size() - 1; // Subtract 1 because the first time point is the start time
int remaining = m_total - completed;

// Calculate durations and moving average
double avg = 0.0;
for (int i = std::max(1, completed - m_window + 1); i <= completed; ++i) {
avg += std::chrono::duration_cast<std::chrono::duration<double>>(m_time_points[i] - m_time_points[i - 1]).count();
}
avg /= std::min(completed, m_window);

// Estimate remaining time
m_average_time = std::chrono::duration<double>(avg);
m_estimated_time_remaining = std::chrono::duration<double>(avg * remaining);
}
if (m_time_points.size() < 2)
return;

int remaining = m_total - m_current_progress;

double avg = 0.0;
size_t window_start = std::max<size_t>(1, m_time_points.size() - m_window);

std::chrono::duration<double> ProgressTracker::time_taken() const {
return m_time_points.back() - m_time_points.front();
for (size_t i = window_start; i < m_time_points.size(); ++i) {
avg += std::chrono::duration_cast<std::chrono::duration<double>>(
m_time_points[i] - m_time_points[i - 1])
.count();
}

avg /= (m_time_points.size() - window_start);

// Estimate remaining time
m_average_time = std::chrono::duration<double>(avg);
m_estimated_time_remaining = std::chrono::duration<double>(avg * remaining);
}

std::chrono::duration<double> ProgressTracker::time_taken() const {
return m_time_points.back() - m_time_points.front();
}

} // namespace occ::core
11 changes: 3 additions & 8 deletions src/crystal/dimer_labeller.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,18 @@
namespace occ::crystal {

std::string SymmetryDimerLabeller::format_symop_with_translation(
const SymmetryOperation &symop, const IVec3 &translation) {
SymmetryOperationFormat format{
"{:>6s}", // fmt string
"," // delimiter
};

const SymmetryOperation &symop, const IVec3 &translation) const {
auto s = symop.translated(translation.cast<double>());
return s.to_string(format);
}

std::string
SymmetryDimerLabeller::format_molecule_part(const std::string &name,
const SymmetryOperation &symop,
const IVec3 &translation) {
const IVec3 &translation) const {
if (symop.is_identity() && translation.isZero())
return name;
return fmt::format("{}[{}]", name,
return fmt::format("{}({})", name,
format_symop_with_translation(symop, translation));
}

Expand Down
Loading

0 comments on commit fccd0cf

Please sign in to comment.