From 45b980194100b31f2fd393899f88a2eebe6d5357 Mon Sep 17 00:00:00 2001 From: Axel Kohlmeyer Date: Fri, 11 Aug 2023 21:59:51 -0400 Subject: [PATCH] add parser that collects info about possible variables to be set from the command line --- tools/lammps-gui/CMakeLists.txt | 2 +- tools/lammps-gui/TODO.md | 1 - tools/lammps-gui/lammpsgui.cpp | 56 +++++++++++++++++++++++++++++++++ tools/lammps-gui/lammpsgui.h | 3 ++ tools/lammps-gui/lammpsgui.ui | 17 ++++++++-- 5 files changed, 74 insertions(+), 5 deletions(-) diff --git a/tools/lammps-gui/CMakeLists.txt b/tools/lammps-gui/CMakeLists.txt index d936bf82d9..fbc55689c0 100644 --- a/tools/lammps-gui/CMakeLists.txt +++ b/tools/lammps-gui/CMakeLists.txt @@ -1,6 +1,6 @@ cmake_minimum_required(VERSION 3.16) -project(lammps-gui VERSION 1.1.6 LANGUAGES CXX) +project(lammps-gui VERSION 1.1.7 LANGUAGES CXX) set(CMAKE_AUTOUIC ON) set(CMAKE_AUTOMOC ON) diff --git a/tools/lammps-gui/TODO.md b/tools/lammps-gui/TODO.md index 7af492888e..7e634b2234 100644 --- a/tools/lammps-gui/TODO.md +++ b/tools/lammps-gui/TODO.md @@ -11,7 +11,6 @@ LAMMPS-GUI TODO list: - need to handle "label" and "jump" commands from within ? - switch processing of input to line based commands or? - switch input file editor to read-only while loop is running -- add Variables menu entry where command line variables can be set - embed all icons/graphics # Long term ideas diff --git a/tools/lammps-gui/lammpsgui.cpp b/tools/lammps-gui/lammpsgui.cpp index 76f1d9f753..01c1fdb600 100644 --- a/tools/lammps-gui/lammpsgui.cpp +++ b/tools/lammps-gui/lammpsgui.cpp @@ -303,6 +303,60 @@ void LammpsGui::update_recents(const QString &filename) } } +void LammpsGui::update_variables() +{ + const auto doc = ui->textEdit->toPlainText().split('\n'); + QStringList known; + QRegularExpression indexvar("^\\s*variable\\s+(\\w+)\\s+index\\s+(.*)"); + QRegularExpression anyvar("^\\s*variable\\s+(\\w+)\\s+(\\w+)\\s+(.*)"); + QRegularExpression usevar("(\\$(\\w)|\\${(\\w+)})"); + QRegularExpression refvar("v_(\\w+)"); + + // forget previously listed variables + variables.clear(); + + for (const auto &line : doc) { + + // first find variable definitions. + // index variables are special since they can be overridden from the command line + auto index = indexvar.match(line); + auto any = anyvar.match(line); + + if (index.hasMatch()) { + if (index.lastCapturedIndex() >= 2) { + auto name = index.captured(1); + if (!known.contains(name)) { + variables.append(qMakePair(name, index.captured(2))); + known.append(name); + } + } + } else if (any.hasMatch()) { + if (any.lastCapturedIndex() >= 3) { + auto name = any.captured(1); + if (!known.contains(name)) known.append(name); + } + } + + // now split line into words and search for use of undefined variables + auto words = line.split(' '); + for (const auto &word : words) { + auto use = usevar.match(word); + auto ref = refvar.match(word); + if (use.hasMatch()) { + auto name = use.captured(use.lastCapturedIndex()); + if (!known.contains(name)) { + known.append(name); + variables.append(qMakePair(name, QString())); + } + } + if (ref.hasMatch()) { + auto name = ref.captured(use.lastCapturedIndex()); + if (!known.contains(name)) known.append(name); + } + } + } +} + // open file and switch CWD to path of file void LammpsGui::open_file(const QString &fileName) { @@ -351,6 +405,8 @@ void LammpsGui::open_file(const QString &fileName) ui->textEdit->document()->setModified(false); file.close(); dirstatus->setText(QString(" Directory: ") + current_dir); + + update_variables(); } void LammpsGui::write_file(const QString &fileName) diff --git a/tools/lammps-gui/lammpsgui.h b/tools/lammps-gui/lammpsgui.h index f2a0fbbbc2..fe78becbe9 100644 --- a/tools/lammps-gui/lammpsgui.h +++ b/tools/lammps-gui/lammpsgui.h @@ -17,6 +17,7 @@ #include #include +#include #include #include @@ -57,6 +58,7 @@ protected: void open_file(const QString &filename); void write_file(const QString &filename); void update_recents(const QString &filename = ""); + void update_variables(); void start_lammps(); void run_done(); @@ -105,6 +107,7 @@ private: QString current_file; QString current_dir; QList recent; + QList> variables; LammpsWrapper lammps; std::string plugin_path; diff --git a/tools/lammps-gui/lammpsgui.ui b/tools/lammps-gui/lammpsgui.ui index 69fa4946b4..3617017804 100644 --- a/tools/lammps-gui/lammpsgui.ui +++ b/tools/lammps-gui/lammpsgui.ui @@ -70,6 +70,8 @@ + + @@ -296,10 +298,10 @@ .. - &View Snapshot + View &Image - View Snapshot of current LAMMPS state + View Snapshot image of current LAMMPS state Ctrl+I @@ -407,12 +409,21 @@ - + + .. &Image Window + + + + + + Set &Variables + +