From 3bf70c1b977e53ee685f64c66691ec40eef35a01 Mon Sep 17 00:00:00 2001 From: Axel Kohlmeyer Date: Tue, 15 Aug 2023 01:05:54 -0400 Subject: [PATCH] various chart window updates and improvements - charts are reset if the thermo style changes - charts are reset if the timestep is reset (to a lower value) - charts are not updated if thermo data is not valid (e.g. during setup) - use integer format for Steps on the x-axis --- tools/lammps-gui/chartviewer.cpp | 26 ++++++++++++++++++++++++++ tools/lammps-gui/chartviewer.h | 8 +++++++- tools/lammps-gui/lammpsgui.cpp | 32 +++++++++++++++++++++++++++++--- 3 files changed, 62 insertions(+), 4 deletions(-) diff --git a/tools/lammps-gui/chartviewer.cpp b/tools/lammps-gui/chartviewer.cpp index 6d9bb02302..0b269a80ec 100644 --- a/tools/lammps-gui/chartviewer.cpp +++ b/tools/lammps-gui/chartviewer.cpp @@ -49,6 +49,31 @@ ChartWindow::ChartWindow(const QString &_filename, QWidget *parent) : resize(settings.value("chartx", 500).toInt(), settings.value("charty", 320).toInt()); } +int ChartWindow::get_step() const +{ + if (charts.size() > 0) { + auto *v = charts[0]; + return (int)v->get_step(v->get_count()-1); + } else { + return -1; + } +} + +void ChartWindow::reset_charts() +{ + while (layout()->count() > 1) { + auto *item = layout()->takeAt(1); + if (item) { + layout()->removeItem(item); + delete item->widget(); + delete item; + } + } + charts.clear(); + columns->clear(); + active_chart = 0; +} + void ChartWindow::add_chart(const QString &title, int index) { auto *chart = new ChartViewer(title, index); @@ -182,6 +207,7 @@ ChartViewer::ChartViewer(const QString &title, int _index, QWidget *parent) : series->attachAxis(yaxis); xaxis->setTitleText("Time step"); xaxis->setTickCount(5); + xaxis->setLabelFormat("%d"); yaxis->setTickCount(5); xaxis->setMinorTickCount(5); yaxis->setMinorTickCount(5); diff --git a/tools/lammps-gui/chartviewer.h b/tools/lammps-gui/chartviewer.h index e440330b2b..22f52a82d6 100644 --- a/tools/lammps-gui/chartviewer.h +++ b/tools/lammps-gui/chartviewer.h @@ -31,7 +31,13 @@ class ChartWindow : public QWidget { public: ChartWindow(const QString &filename, QWidget *parent = nullptr); - bool has_charts() const { return !charts.isEmpty(); } + int num_charts() const { return charts.size(); } + bool has_title(const QString &title, int index) const + { + return (columns->itemText(index) == title); + } + int get_step() const; + void reset_charts(); void add_chart(const QString &title, int index); void add_data(int step, double data, int index); diff --git a/tools/lammps-gui/lammpsgui.cpp b/tools/lammps-gui/lammpsgui.cpp index 1a27b7c743..385e3331b4 100644 --- a/tools/lammps-gui/lammpsgui.cpp +++ b/tools/lammps-gui/lammpsgui.cpp @@ -558,7 +558,11 @@ void LammpsGui::logupdate() // extract cached thermo data if (chartwindow) { - void *ptr = lammps.last_thermo("step", 0); + // thermo data is not yet valid during setup + void *ptr = lammps.last_thermo("setup", 0); + if (ptr && *(int *)ptr) return; + + ptr = lammps.last_thermo("step", 0); if (ptr) { int step = 0; if (lammps.extract_setting("bigint") == 4) @@ -566,7 +570,29 @@ void LammpsGui::logupdate() else step = (int)*(int64_t *)ptr; int ncols = *(int *)lammps.last_thermo("num", 0); - if (!chartwindow->has_charts()) { + + // check if the column assignment has changed + // if yes, delete charts and start over + if (chartwindow->num_charts() > 0) { + int count = 0; + bool do_reset = false; + if (step < chartwindow->get_step()) do_reset = true; + for (int i = 0, idx = 0; i < ncols; ++i) { + QString label = (const char *)lammps.last_thermo("keyword", i); + // no need to store the timestep column + if (label == "Step") continue; + if (!chartwindow->has_title(label, idx)) { + do_reset = true; + } else { + ++count; + } + ++idx; + } + if (chartwindow->num_charts() != count) do_reset = true; + if (do_reset) chartwindow->reset_charts(); + } + + if (chartwindow->num_charts() == 0) { for (int i = 0; i < ncols; ++i) { QString label = (const char *)lammps.last_thermo("keyword", i); // no need to store the timestep column @@ -622,7 +648,7 @@ void LammpsGui::run_done() step = (int)*(int64_t *)ptr; int ncols = *(int *)lammps.last_thermo("num", 0); for (int i = 0; i < ncols; ++i) { - if (!chartwindow->has_charts()) { + if (chartwindow->num_charts() == 0) { QString label = (const char *)lammps.last_thermo("keyword", i); // no need to store the timestep column if (label == "Step") continue;