add parser that collects info about possible variables to be set from the command line

This commit is contained in:
Axel Kohlmeyer
2023-08-11 21:59:51 -04:00
parent 2cf73ff427
commit 45b9801941
5 changed files with 74 additions and 5 deletions

View File

@ -1,6 +1,6 @@
cmake_minimum_required(VERSION 3.16) 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_AUTOUIC ON)
set(CMAKE_AUTOMOC ON) set(CMAKE_AUTOMOC ON)

View File

@ -11,7 +11,6 @@ LAMMPS-GUI TODO list:
- need to handle "label" and "jump" commands from within ? - need to handle "label" and "jump" commands from within ?
- switch processing of input to line based commands or? - switch processing of input to line based commands or?
- switch input file editor to read-only while loop is running - 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 - embed all icons/graphics
# Long term ideas # Long term ideas

View File

@ -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 // open file and switch CWD to path of file
void LammpsGui::open_file(const QString &fileName) void LammpsGui::open_file(const QString &fileName)
{ {
@ -351,6 +405,8 @@ void LammpsGui::open_file(const QString &fileName)
ui->textEdit->document()->setModified(false); ui->textEdit->document()->setModified(false);
file.close(); file.close();
dirstatus->setText(QString(" Directory: ") + current_dir); dirstatus->setText(QString(" Directory: ") + current_dir);
update_variables();
} }
void LammpsGui::write_file(const QString &fileName) void LammpsGui::write_file(const QString &fileName)

View File

@ -17,6 +17,7 @@
#include <QMainWindow> #include <QMainWindow>
#include <QList> #include <QList>
#include <QPair>
#include <QString> #include <QString>
#include <vector> #include <vector>
@ -57,6 +58,7 @@ protected:
void open_file(const QString &filename); void open_file(const QString &filename);
void write_file(const QString &filename); void write_file(const QString &filename);
void update_recents(const QString &filename = ""); void update_recents(const QString &filename = "");
void update_variables();
void start_lammps(); void start_lammps();
void run_done(); void run_done();
@ -105,6 +107,7 @@ private:
QString current_file; QString current_file;
QString current_dir; QString current_dir;
QList<QString> recent; QList<QString> recent;
QList<QPair<QString, QString>> variables;
LammpsWrapper lammps; LammpsWrapper lammps;
std::string plugin_path; std::string plugin_path;

View File

@ -70,6 +70,8 @@
<addaction name="actionRun_Buffer"/> <addaction name="actionRun_Buffer"/>
<addaction name="actionStop_LAMMPS"/> <addaction name="actionStop_LAMMPS"/>
<addaction name="separator"/> <addaction name="separator"/>
<addaction name="actionSet_Variables"/>
<addaction name="separator"/>
<addaction name="actionImage"/> <addaction name="actionImage"/>
</widget> </widget>
<widget class="QMenu" name="menuAbout"> <widget class="QMenu" name="menuAbout">
@ -296,10 +298,10 @@
<normaloff>.</normaloff>.</iconset> <normaloff>.</normaloff>.</iconset>
</property> </property>
<property name="text"> <property name="text">
<string>&amp;View Snapshot</string> <string>View &amp;Image</string>
</property> </property>
<property name="toolTip"> <property name="toolTip">
<string>View Snapshot of current LAMMPS state</string> <string>View Snapshot image of current LAMMPS state</string>
</property> </property>
<property name="shortcut"> <property name="shortcut">
<string>Ctrl+I</string> <string>Ctrl+I</string>
@ -407,12 +409,21 @@
</action> </action>
<action name="actionView_Image_Window"> <action name="actionView_Image_Window">
<property name="icon"> <property name="icon">
<iconset theme="emblem-photos"/> <iconset theme="emblem-photos">
<normaloff>.</normaloff>.</iconset>
</property> </property>
<property name="text"> <property name="text">
<string>&amp;Image Window</string> <string>&amp;Image Window</string>
</property> </property>
</action> </action>
<action name="actionSet_Variables">
<property name="icon">
<iconset theme="preferences-desktop-personal"/>
</property>
<property name="text">
<string>Set &amp;Variables</string>
</property>
</action>
</widget> </widget>
<customwidgets> <customwidgets>
<customwidget> <customwidget>