add support to run LAMMPS on the file instead of the edit buffer

This commit is contained in:
Axel Kohlmeyer
2023-08-25 15:12:48 -04:00
parent f94d00d435
commit c61aaa81d2
7 changed files with 123 additions and 53 deletions

View File

@ -211,6 +211,7 @@ LammpsGui::LammpsGui(QWidget *parent, const char *filename) :
connect(ui->actionUndo, &QAction::triggered, this, &LammpsGui::undo); connect(ui->actionUndo, &QAction::triggered, this, &LammpsGui::undo);
connect(ui->actionRedo, &QAction::triggered, this, &LammpsGui::redo); connect(ui->actionRedo, &QAction::triggered, this, &LammpsGui::redo);
connect(ui->actionRun_Buffer, &QAction::triggered, this, &LammpsGui::run_buffer); connect(ui->actionRun_Buffer, &QAction::triggered, this, &LammpsGui::run_buffer);
connect(ui->actionRun_File, &QAction::triggered, this, &LammpsGui::run_file);
connect(ui->actionStop_LAMMPS, &QAction::triggered, this, &LammpsGui::stop_run); connect(ui->actionStop_LAMMPS, &QAction::triggered, this, &LammpsGui::stop_run);
connect(ui->actionSet_Variables, &QAction::triggered, this, &LammpsGui::edit_variables); connect(ui->actionSet_Variables, &QAction::triggered, this, &LammpsGui::edit_variables);
connect(ui->actionImage, &QAction::triggered, this, &LammpsGui::render_image); connect(ui->actionImage, &QAction::triggered, this, &LammpsGui::render_image);
@ -810,13 +811,34 @@ void LammpsGui::run_done()
dirstatus->show(); dirstatus->show();
} }
void LammpsGui::run_buffer() void LammpsGui::do_run(bool use_buffer)
{ {
if (lammps.is_running()) { if (lammps.is_running()) {
QMessageBox::warning(this, "LAMMPS GUI Error", QMessageBox::warning(this, "LAMMPS GUI Error",
"Must stop current run before starting a new run"); "Must stop current run before starting a new run");
return; return;
} }
if (!use_buffer && ui->textEdit->document()->isModified()) {
QMessageBox msg;
msg.setWindowTitle("Unsaved Changes");
msg.setWindowIcon(windowIcon());
msg.setText(QString("The buffer ") + current_file + " has changes");
msg.setInformativeText("Do you want to save the buffer before running LAMMPS?");
msg.setIcon(QMessageBox::Question);
msg.setStandardButtons(QMessageBox::Yes | QMessageBox::Cancel);
int rv = msg.exec();
switch (rv) {
case QMessageBox::Yes:
save();
break;
case QMessageBox::Cancel: // falthrough
default:
return;
break;
}
}
QSettings settings; QSettings settings;
progress->setValue(0); progress->setValue(0);
dirstatus->hide(); dirstatus->hide();
@ -836,12 +858,17 @@ void LammpsGui::run_buffer()
clear(); clear();
capturer->BeginCapture(); capturer->BeginCapture();
// always add final newline since the text edit widget does not runner = new LammpsRunner(this);
char *input = mystrdup(ui->textEdit->toPlainText().toStdString() + "\n"); is_running = true;
is_running = true; if (use_buffer) {
// always add final newline since the text edit widget does not
char *input = mystrdup(ui->textEdit->toPlainText().toStdString() + "\n");
runner->setup_run(&lammps, input, nullptr);
} else {
char *fname = mystrdup(current_file.toStdString());
runner->setup_run(&lammps, nullptr, fname);
}
runner = new LammpsRunner(this);
runner->setup_run(&lammps, input);
connect(runner, &LammpsRunner::resultReady, this, &LammpsGui::run_done); connect(runner, &LammpsRunner::resultReady, this, &LammpsGui::run_done);
connect(runner, &LammpsRunner::finished, runner, &QObject::deleteLater); connect(runner, &LammpsRunner::finished, runner, &QObject::deleteLater);
runner->start(); runner->start();
@ -852,8 +879,12 @@ void LammpsGui::run_buffer()
logwindow->setReadOnly(true); logwindow->setReadOnly(true);
logwindow->setCenterOnScroll(true); logwindow->setCenterOnScroll(true);
logwindow->moveCursor(QTextCursor::End); logwindow->moveCursor(QTextCursor::End);
logwindow->setWindowTitle("LAMMPS-GUI - Output from running LAMMPS on buffer - " + if (use_buffer)
current_file); logwindow->setWindowTitle("LAMMPS-GUI - Output from running LAMMPS on buffer - " +
current_file);
else
logwindow->setWindowTitle("LAMMPS-GUI - Output from running LAMMPS on file - " +
current_file);
logwindow->setWindowIcon(QIcon(":/lammps-icon-128x128.png")); logwindow->setWindowIcon(QIcon(":/lammps-icon-128x128.png"));
QFont text_font; QFont text_font;
text_font.fromString(settings.value("textfont", text_font.toString()).toString()); text_font.fromString(settings.value("textfont", text_font.toString()).toString());
@ -872,8 +903,12 @@ void LammpsGui::run_buffer()
// if configured, delete old log window before opening new one // if configured, delete old log window before opening new one
if (settings.value("chartreplace", false).toBool()) delete chartwindow; if (settings.value("chartreplace", false).toBool()) delete chartwindow;
chartwindow = new ChartWindow(current_file); chartwindow = new ChartWindow(current_file);
chartwindow->setWindowTitle("LAMMPS-GUI - Thermo charts from running LAMMPS on buffer - " + if (use_buffer)
current_file); chartwindow->setWindowTitle("LAMMPS-GUI - Thermo charts from running LAMMPS on buffer - " +
current_file);
else
chartwindow->setWindowTitle("LAMMPS-GUI - Thermo charts from running LAMMPS on file - " +
current_file);
chartwindow->setWindowIcon(QIcon(":/lammps-icon-128x128.png")); chartwindow->setWindowIcon(QIcon(":/lammps-icon-128x128.png"));
chartwindow->setMinimumSize(400, 300); chartwindow->setMinimumSize(400, 300);
shortcut = new QShortcut(QKeySequence(Qt::CTRL + Qt::Key_W), chartwindow); shortcut = new QShortcut(QKeySequence(Qt::CTRL + Qt::Key_W), chartwindow);
@ -1092,8 +1127,8 @@ void LammpsGui::preferences()
QSettings settings; QSettings settings;
int oldthreads = settings.value("nthreads", 1).toInt(); int oldthreads = settings.value("nthreads", 1).toInt();
int oldaccel = settings.value("accelerator", AcceleratorTab::None).toInt(); int oldaccel = settings.value("accelerator", AcceleratorTab::None).toInt();
bool oldecho = settings.value("echo", 0).toBool(); bool oldecho = settings.value("echo", 0).toBool();
bool oldcite = settings.value("cite", 0).toBool(); bool oldcite = settings.value("cite", 0).toBool();
Preferences prefs(&lammps); Preferences prefs(&lammps);
if (prefs.exec() == QDialog::Accepted) { if (prefs.exec() == QDialog::Accepted) {

View File

@ -60,6 +60,7 @@ protected:
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 update_variables();
void do_run(bool use_buffer);
void start_lammps(); void start_lammps();
void run_done(); void run_done();
@ -77,7 +78,9 @@ private slots:
void undo(); void undo();
void redo(); void redo();
void clear(); void clear();
void run_buffer(); void run_buffer() { do_run(true); }
void run_file() { do_run(false); }
void stop_run(); void stop_run();
void edit_variables(); void edit_variables();
void render_image(); void render_image();

View File

@ -1,48 +1,49 @@
<RCC> <RCC>
<qresource prefix="/"> <qresource prefix="/">
<file>lammps-icon-128x128.png</file> <file>lammps-icon-128x128.png</file>
<file>help_index.table</file> <file>antialias.png</file>
<file>system-help.png</file> <file>application-calc.png</file>
<file>help-browser.png</file> <file>application-exit.png</file>
<file>gtk-zoom-in.png</file> <file>application-plot.png</file>
<file>gtk-zoom-out.png</file> <file>axes-img.png</file>
<file>gtk-zoom-fit.png</file> <file>document-new.png</file>
<file>edit-delete.png</file>
<file>object-rotate-right.png</file>
<file>object-rotate-left.png</file>
<file>gtk-go-up.png</file>
<file>gtk-go-down.png</file>
<file>document-save-as.png</file>
<file>document-open-recent.png</file> <file>document-open-recent.png</file>
<file>document-open.png</file> <file>document-open.png</file>
<file>document-new.png</file>
<file>edit-undo.png</file>
<file>edit-redo.png</file>
<file>edit-paste.png</file>
<file>edit-cut.png</file>
<file>edit-copy.png</file>
<file>application-exit.png</file>
<file>utilities-terminal.png</file>
<file>x-office-drawing.png</file>
<file>document-save.png</file>
<file>document-revert.png</file> <file>document-revert.png</file>
<file>preferences-desktop.png</file> <file>document-save-as.png</file>
<file>preferences-desktop-personal.png</file> <file>document-save.png</file>
<file>preferences-desktop-font.png</file> <file>edit-copy.png</file>
<file>help-faq.png</file> <file>edit-cut.png</file>
<file>help-about.png</file> <file>edit-delete.png</file>
<file>edit-paste.png</file>
<file>edit-redo.png</file>
<file>edit-undo.png</file>
<file>emblem-photos.png</file> <file>emblem-photos.png</file>
<file>process-stop.png</file> <file>gtk-go-down.png</file>
<file>system-run.png</file> <file>gtk-go-up.png</file>
<file>window-close.png</file> <file>gtk-zoom-fit.png</file>
<file>application-plot.png</file> <file>gtk-zoom-in.png</file>
<file>application-calc.png</file> <file>gtk-zoom-out.png</file>
<file>system-box.png</file>
<file>axes-img.png</file>
<file>hd-img.png</file> <file>hd-img.png</file>
<file>antialias.png</file> <file>help-about.png</file>
<file>help-browser.png</file>
<file>help-faq.png</file>
<file>help_index.table</file>
<file>object-rotate-left.png</file>
<file>object-rotate-right.png</file>
<file>ovito.png</file> <file>ovito.png</file>
<file>vmd.png</file> <file>preferences-desktop-font.png</file>
<file>preferences-desktop-personal.png</file>
<file>preferences-desktop.png</file>
<file>process-stop.png</file>
<file>run-file.png</file>
<file>system-box.png</file>
<file>system-help.png</file>
<file>system-run.png</file>
<file>utilities-terminal.png</file>
<file>vdw-style.png</file> <file>vdw-style.png</file>
<file>vmd.png</file>
<file>window-close.png</file>
<file>x-office-drawing.png</file>
</qresource> </qresource>
</RCC> </RCC>

View File

@ -68,6 +68,7 @@
<string>&amp;Run</string> <string>&amp;Run</string>
</property> </property>
<addaction name="actionRun_Buffer"/> <addaction name="actionRun_Buffer"/>
<addaction name="actionRun_File"/>
<addaction name="actionStop_LAMMPS"/> <addaction name="actionStop_LAMMPS"/>
<addaction name="separator"/> <addaction name="separator"/>
<addaction name="actionSet_Variables"/> <addaction name="actionSet_Variables"/>
@ -215,12 +216,23 @@
<iconset theme=":/system-run.png"/> <iconset theme=":/system-run.png"/>
</property> </property>
<property name="text"> <property name="text">
<string>&amp;Run LAMMPS</string> <string>&amp;Run LAMMPS from Editor Buffer</string>
</property> </property>
<property name="shortcut"> <property name="shortcut">
<string>Ctrl+Return</string> <string>Ctrl+Return</string>
</property> </property>
</action> </action>
<action name="actionRun_File">
<property name="icon">
<iconset theme=":/run-file.png"/>
</property>
<property name="text">
<string>&amp;Run LAMMPS from File</string>
</property>
<property name="shortcut">
<string>Ctrl+Shift+Return</string>
</property>
</action>
<action name="actionStop_LAMMPS"> <action name="actionStop_LAMMPS">
<property name="icon"> <property name="icon">
<iconset theme=":/process-stop.png"/> <iconset theme=":/process-stop.png"/>

View File

@ -27,16 +27,22 @@ public:
// execute LAMMPS in runner thread // execute LAMMPS in runner thread
void run() override void run() override
{ {
lammps->commands_string(input); if (input) {
delete[] input; lammps->commands_string(input);
delete[] input;
} else if (file) {
lammps->file(file);
delete[] file;
}
emit resultReady(); emit resultReady();
} }
// transfer info to worker thread and reset LAMMPS instance // transfer info to worker thread and reset LAMMPS instance
void setup_run(LammpsWrapper *_lammps, const char *_input) void setup_run(LammpsWrapper *_lammps, const char *_input, const char *_file = nullptr)
{ {
lammps = _lammps; lammps = _lammps;
input = _input; input = _input;
file = _file;
lammps->command("clear"); lammps->command("clear");
} }
@ -46,6 +52,7 @@ signals:
private: private:
LammpsWrapper *lammps; LammpsWrapper *lammps;
const char *input; const char *input;
const char *file;
}; };
#endif #endif

View File

@ -147,6 +147,17 @@ void LammpsWrapper::command(const char *input)
} }
} }
void LammpsWrapper::file(const char *filename)
{
if (lammps_handle) {
#if defined(LAMMPS_GUI_USE_PLUGIN)
((liblammpsplugin_t *)plugin_handle)->file(lammps_handle, filename);
#else
lammps_file(lammps_handle, filename);
#endif
}
}
void LammpsWrapper::commands_string(const char *input) void LammpsWrapper::commands_string(const char *input)
{ {
if (lammps_handle) { if (lammps_handle) {

View File

@ -23,6 +23,7 @@ public:
void close(); void close();
void finalize(); void finalize();
void file(const char *);
void command(const char *); void command(const char *);
void commands_string(const char *); void commands_string(const char *);