refactor. add LammpsWrapper class to hide plugin defines and interface changes.

This commit is contained in:
Axel Kohlmeyer
2023-08-02 01:10:49 -04:00
parent 310ede65d9
commit 8a53c66bef
7 changed files with 327 additions and 195 deletions

View File

@ -76,8 +76,9 @@ set(PROJECT_SOURCES
lammpsgui.cpp lammpsgui.cpp
lammpsgui.h lammpsgui.h
lammpsgui.ui lammpsgui.ui
lammpsrunner.cpp
lammpsrunner.h lammpsrunner.h
lammpswrapper.cpp
lammpswrapper.h
preferences.cpp preferences.cpp
preferences.h preferences.h
stdcapture.cpp stdcapture.cpp

View File

@ -20,6 +20,7 @@
#include "stdcapture.h" #include "stdcapture.h"
#include "ui_lammpsgui.h" #include "ui_lammpsgui.h"
#include <QCoreApplication>
#include <QDesktopServices> #include <QDesktopServices>
#include <QFileDialog> #include <QFileDialog>
#include <QFileInfo> #include <QFileInfo>
@ -28,21 +29,17 @@
#include <QMessageBox> #include <QMessageBox>
#include <QPlainTextEdit> #include <QPlainTextEdit>
#include <QProgressBar> #include <QProgressBar>
#include <QSettings>
#include <QShortcut> #include <QShortcut>
#include <QStatusBar> #include <QStatusBar>
#include <QTextStream> #include <QTextStream>
#include <QThread>
#include <QTimer> #include <QTimer>
#include <QUrl> #include <QUrl>
#include <cstring> #include <cstring>
#include <string> #include <string>
#if defined(LAMMPS_GUI_USE_PLUGIN)
#include "liblammpsplugin.h"
#else
#include "library.h"
#endif
#if defined(_OPENMP) #if defined(_OPENMP)
#include <cstdlib> #include <cstdlib>
#include <omp.h> #include <omp.h>
@ -54,6 +51,8 @@
#include <unistd.h> #include <unistd.h>
#endif #endif
static const QString blank(" ");
// duplicate string // duplicate string
static char *mystrdup(const std::string &text) static char *mystrdup(const std::string &text)
{ {
@ -65,8 +64,7 @@ static char *mystrdup(const std::string &text)
LammpsGui::LammpsGui(QWidget *parent, const char *filename) : LammpsGui::LammpsGui(QWidget *parent, const char *filename) :
QMainWindow(parent), ui(new Ui::LammpsGui), highlighter(nullptr), capturer(nullptr), QMainWindow(parent), ui(new Ui::LammpsGui), highlighter(nullptr), capturer(nullptr),
status(nullptr), logwindow(nullptr), imagewindow(nullptr), logupdater(nullptr), status(nullptr), logwindow(nullptr), imagewindow(nullptr), logupdater(nullptr),
dirstatus(nullptr), progress(nullptr), prefdialog(nullptr), lammps_handle(nullptr), dirstatus(nullptr), progress(nullptr), prefdialog(nullptr)
plugin_handle(nullptr), plugin_path(nullptr), is_running(false)
{ {
ui->setupUi(this); ui->setupUi(this);
this->setCentralWidget(ui->textEdit); this->setCentralWidget(ui->textEdit);
@ -77,9 +75,16 @@ LammpsGui::LammpsGui(QWidget *parent, const char *filename) :
current_dir = QDir(".").absolutePath(); current_dir = QDir(".").absolutePath();
recent_files.clear(); recent_files.clear();
QCoreApplication::setOrganizationName("The LAMMPS Developers");
QCoreApplication::setOrganizationDomain("lammps.org");
QCoreApplication::setApplicationName("LAMMPS GUI");
// restorge and initialize settings
QSettings settings;
#if defined(_OPENMP) #if defined(_OPENMP)
// use maximum number of available threads unless OMP_NUM_THREADS was set // use maximum number of available threads unless OMP_NUM_THREADS was set
nthreads = omp_get_max_threads(); nthreads = settings.value("nthreads", omp_get_max_threads()).toInt();
#if _WIN32 #if _WIN32
if (!getenv("OMP_NUM_THREADS")) { if (!getenv("OMP_NUM_THREADS")) {
_putenv_s("OMP_NUM_THREADS", std::to_string(nthreads).c_str()); _putenv_s("OMP_NUM_THREADS", std::to_string(nthreads).c_str());
@ -88,7 +93,7 @@ LammpsGui::LammpsGui(QWidget *parent, const char *filename) :
setenv("OMP_NUM_THREADS", std::to_string(nthreads).c_str(), 0); setenv("OMP_NUM_THREADS", std::to_string(nthreads).c_str(), 0);
#endif #endif
#else #else
nthreads = 1; nthreads = settings.value("nthreads", 1).toInt();
#endif #endif
const char *tmpdir = getenv("TMPDIR"); const char *tmpdir = getenv("TMPDIR");
@ -100,14 +105,14 @@ LammpsGui::LammpsGui(QWidget *parent, const char *filename) :
#else #else
if (!tmpdir) tmpdir = "/tmp"; if (!tmpdir) tmpdir = "/tmp";
#endif #endif
temp_dir = tmpdir; settings.setValue("tempdir", QString(tmpdir));
lammps_args.clear(); lammps_args.clear();
lammps_args.push_back(mystrdup("LAMMPS-GUI")); lammps_args.push_back(mystrdup("LAMMPS-GUI"));
lammps_args.push_back(mystrdup("-log")); lammps_args.push_back(mystrdup("-log"));
lammps_args.push_back(mystrdup("none")); lammps_args.push_back(mystrdup("none"));
lammps_args.push_back(mystrdup("-suffix")); // lammps_args.push_back(mystrdup("-suffix"));
lammps_args.push_back(mystrdup("omp")); // lammps_args.push_back(mystrdup("omp"));
setWindowIcon(QIcon(":/lammps-icon-128x128.png")); setWindowIcon(QIcon(":/lammps-icon-128x128.png"));
#if (__APPLE__) #if (__APPLE__)
@ -158,24 +163,24 @@ LammpsGui::LammpsGui(QWidget *parent, const char *filename) :
dirstatus->show(); dirstatus->show();
ui->statusbar->addWidget(progress); ui->statusbar->addWidget(progress);
#if defined(LAMMPS_GUI_USE_PLUGIN) plugin_path.clear();
liblammpsplugin_t *lammps = nullptr; std::string deffile = settings.value("plugin_path", "liblammps.so").toString().toStdString();
for (const auto libfile : {"liblammps.so", "./liblammps.so", "liblammps.dylib", for (const char *libfile : {deffile.c_str(), "./liblammps.so", "liblammps.dylib",
"./liblammps.dylib", "liblammps.dll"}) { "./liblammps.dylib", "liblammps.dll"}) {
if (!lammps) lammps = liblammpsplugin_load(libfile); if (lammps.load_lib(libfile)) {
if (lammps) {
plugin_path = libfile; plugin_path = libfile;
settings.setValue("plugin_path", QString(libfile));
break; break;
} }
} }
bool do_exit = !lammps || (lammps && lammps->abiversion != LAMMPSPLUGIN_ABI_VERSION);
if (!lammps) QMessageBox::critical(this, "Error", "Cannot open LAMMPS shared library file"); if (plugin_path.empty()) {
if (lammps && (lammps->abiversion != LAMMPSPLUGIN_ABI_VERSION)) // none of the plugin paths could load, remove key
QMessageBox::critical(this, "Warning", settings.remove("plugin_path");
"ERROR: LAMMPS lib plugin ABI version does not match"); QMessageBox::critical(this, "Error", "Cannot open LAMMPS shared library file");
plugin_handle = lammps; // QCoreApplication::quit();
if (do_exit) exit(1); exit(1);
#endif }
if (filename) { if (filename) {
open_file(filename); open_file(filename);
@ -200,12 +205,7 @@ void LammpsGui::new_document()
current_file.clear(); current_file.clear();
ui->textEdit->document()->setPlainText(QString()); ui->textEdit->document()->setPlainText(QString());
#if defined(LAMMPS_GUI_USE_PLUGIN) lammps.close();
if (lammps_handle) ((liblammpsplugin_t *)plugin_handle)->close(lammps_handle);
#else
if (lammps_handle) lammps_close(lammps_handle);
#endif
lammps_handle = nullptr;
setWindowTitle(QString("LAMMPS-GUI - *unknown*")); setWindowTitle(QString("LAMMPS-GUI - *unknown*"));
} }
@ -300,22 +300,9 @@ void LammpsGui::save_as()
void LammpsGui::quit() void LammpsGui::quit()
{ {
#if defined(LAMMPS_GUI_USE_PLUGIN) lammps.close();
if (lammps_handle) { lammps.finalize();
liblammpsplugin_t *lammps = (liblammpsplugin_t *)plugin_handle;
lammps->close(lammps_handle);
lammps->mpi_finalize();
lammps->kokkos_finalize();
lammps->python_finalize();
}
#else
if (lammps_handle) {
lammps_close(lammps_handle);
lammps_mpi_finalize();
lammps_kokkos_finalize();
lammps_python_finalize();
}
#endif
if (ui->textEdit->document()->isModified()) { if (ui->textEdit->document()->isModified()) {
QMessageBox msg; QMessageBox msg;
msg.setWindowTitle("Unsaved Changes"); msg.setWindowTitle("Unsaved Changes");
@ -374,11 +361,7 @@ void LammpsGui::redo()
void LammpsGui::stop_run() void LammpsGui::stop_run()
{ {
#if defined(LAMMPS_GUI_USE_PLUGIN) lammps.force_timeout();
((liblammpsplugin_t *)plugin_handle)->force_timeout(lammps_handle);
#else
lammps_force_timeout(lammps_handle);
#endif
} }
void LammpsGui::logupdate() void LammpsGui::logupdate()
@ -386,22 +369,12 @@ void LammpsGui::logupdate()
double t_elapsed, t_remain, t_total; double t_elapsed, t_remain, t_total;
int completed = 1000; int completed = 1000;
#if defined(LAMMPS_GUI_USE_PLUGIN) if (is_running) {
liblammpsplugin_t *lammps = (liblammpsplugin_t *)plugin_handle; t_elapsed = lammps.get_thermo("cpu");
if (lammps->is_running(lammps_handle)) { t_remain = lammps.get_thermo("cpuremain");
t_elapsed = lammps->get_thermo(lammps_handle, "cpu");
t_remain = lammps->get_thermo(lammps_handle, "cpuremain");
t_total = t_elapsed + t_remain + 1.0e-10; t_total = t_elapsed + t_remain + 1.0e-10;
completed = t_elapsed / t_total * 1000.0; completed = t_elapsed / t_total * 1000.0;
} }
#else
if (lammps_is_running(lammps_handle)) {
t_elapsed = lammps_get_thermo(lammps_handle, "cpu");
t_remain = lammps_get_thermo(lammps_handle, "cpuremain");
t_total = t_elapsed + t_remain + 1.0e-10;
completed = t_elapsed / t_total * 1000.0;
}
#endif
progress->setValue(completed); progress->setValue(completed);
if (logwindow) { if (logwindow) {
@ -440,18 +413,10 @@ void LammpsGui::run_done()
constexpr int BUFLEN = 1024; constexpr int BUFLEN = 1024;
char errorbuf[BUFLEN]; char errorbuf[BUFLEN];
#if defined(LAMMPS_GUI_USE_PLUGIN) if (lammps.has_error()) {
liblammpsplugin_t *lammps = (liblammpsplugin_t *)plugin_handle; lammps.get_last_error_message(errorbuf, BUFLEN);
if (lammps->has_error(lammps_handle)) {
lammps->get_last_error_message(lammps_handle, errorbuf, BUFLEN);
success = false; success = false;
} }
#else
if (lammps_has_error(lammps_handle)) {
lammps_get_last_error_message(lammps_handle, errorbuf, BUFLEN);
success = false;
}
#endif
if (success) { if (success) {
status->setText("Ready."); status->setText("Ready.");
@ -460,7 +425,6 @@ void LammpsGui::run_done()
QMessageBox::critical(this, "LAMMPS-GUI Error", QMessageBox::critical(this, "LAMMPS-GUI Error",
QString("Error running LAMMPS:\n\n") + errorbuf); QString("Error running LAMMPS:\n\n") + errorbuf);
} }
is_running = false;
progress->hide(); progress->hide();
dirstatus->show(); dirstatus->show();
} }
@ -473,7 +437,7 @@ void LammpsGui::run_buffer()
status->setText(QString("Running LAMMPS with %1 thread(s)...").arg(nthreads)); status->setText(QString("Running LAMMPS with %1 thread(s)...").arg(nthreads));
status->repaint(); status->repaint();
start_lammps(); start_lammps();
if (!lammps_handle) return; if (!lammps.is_open()) return;
clear(); clear();
capturer->BeginCapture(); capturer->BeginCapture();
@ -481,7 +445,7 @@ void LammpsGui::run_buffer()
is_running = true; is_running = true;
LammpsRunner *runner = new LammpsRunner(this); LammpsRunner *runner = new LammpsRunner(this);
runner->setup_run(lammps_handle, input, plugin_handle); 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();
@ -516,43 +480,39 @@ void LammpsGui::run_buffer()
void LammpsGui::view_image() void LammpsGui::view_image()
{ {
// LAMMPS is not re-entrant, so we can only query LAMMPS when it is not running // LAMMPS is not re-entrant, so we can only query LAMMPS when it is not running
if (!is_running) { if (!lammps.is_running()) {
start_lammps(); start_lammps();
int box = 0; if (!lammps.extract_setting("box_exists")) {
#if defined(LAMMPS_GUI_USE_PLUGIN)
box = ((liblammpsplugin_t *)plugin_handle)->extract_setting(lammps_handle, "box_exists");
#else
box = lammps_extract_setting(lammps_handle, "box_exist");
#endif
if (!box) {
QMessageBox::warning(this, "ImageViewer Error", QMessageBox::warning(this, "ImageViewer Error",
"Cannot create snapshot image without a system box"); "Cannot create snapshot image without a system box");
return; return;
} }
std::string dumpcmd = "write_dump all image '"; QSettings settings;
QString dumpfile = temp_dir; QString dumpcmd = "write_dump all image ";
QString dumpfile = settings.value("tempdir").toString();
#if defined(_WIN32) #if defined(_WIN32)
dumpfile += '\\'; dumpfile += '\\';
#else #else
dumpfile += '/'; dumpfile += '/';
#endif #endif
dumpfile += current_file + ".ppm"; dumpfile += current_file + ".ppm";
dumpcmd += dumpfile;
dumpcmd += dumpfile.toStdString() + "' type type size 800 600"; settings.beginGroup("snapshot");
dumpcmd += blank + settings.value("color", "type").toString();
dumpcmd += blank + settings.value("diameter", "type").toString();
dumpcmd += QString(" size ") + settings.value("xsize", "800").toString();
dumpcmd += blank + settings.value("ysize", "600").toString();
dumpcmd += QString(" zoom ") + settings.value("zoom", "1.0").toString();
settings.endGroup();
#if defined(LAMMPS_GUI_USE_PLUGIN) lammps.command(dumpcmd.toLocal8Bit());
((liblammpsplugin_t *)plugin_handle)->command(lammps_handle, dumpcmd.c_str());
#else
lammps_command(lammps_handle, dumpcmd.c_str());
#endif
imagewindow = new ImageViewer(dumpfile); imagewindow = new ImageViewer(dumpfile);
#if 0
#if defined(_WIN32) #if defined(_WIN32)
_unlink(dumpfile.toLocal8Bit()); _unlink(dumpfile.toLocal8Bit());
#else #else
unlink(dumpfile.toLocal8Bit()); unlink(dumpfile.toLocal8Bit());
#endif
#endif #endif
} else { } else {
QMessageBox::warning(this, "ImageViewer Error", QMessageBox::warning(this, "ImageViewer Error",
@ -564,39 +524,29 @@ void LammpsGui::view_image()
void LammpsGui::clear() void LammpsGui::clear()
{ {
if (lammps_handle) { if (!lammps.is_open()) lammps.command("clear");
#if defined(LAMMPS_GUI_USE_PLUGIN)
((liblammpsplugin_t *)plugin_handle)->command(lammps_handle, "clear");
#else
lammps_command(lammps_handle, "clear");
#endif
}
ui->textEdit->moveCursor(QTextCursor::Start, QTextCursor::MoveAnchor); ui->textEdit->moveCursor(QTextCursor::Start, QTextCursor::MoveAnchor);
} }
void LammpsGui::about() void LammpsGui::about()
{ {
std::string version = "This is LAMMPS-GUI version " LAMMPS_GUI_VERSION; std::string version = "This is LAMMPS-GUI version " LAMMPS_GUI_VERSION;
#if defined(LAMMPS_GUI_USE_PLUGIN) if (lammps.has_plugin()) {
version += " - LAMMPS linked dynamically"; version += " - LAMMPS linked dynamically";
if (plugin_path) { if (!plugin_path.empty()) {
version += " from file "; version += " from file ";
version += plugin_path; version += plugin_path;
} }
#else } else {
version += " - LAMMPS linked statically"; version += " - LAMMPS linked statically";
#endif }
std::string info = "LAMMPS is currently running. LAMMPS config info not available."; std::string info = "LAMMPS is currently running. LAMMPS config info not available.";
// LAMMPS is not re-entrant, so we can only query LAMMPS when it is not running // LAMMPS is not re-entrant, so we can only query LAMMPS when it is not running
if (!is_running) { if (!lammps.is_running()) {
start_lammps(); start_lammps();
capturer->BeginCapture(); capturer->BeginCapture();
#if defined(LAMMPS_GUI_USE_PLUGIN) lammps.command("info config");
((liblammpsplugin_t *)plugin_handle)->commands(lammps_handle, "info config");
#else
lammps_command(lammps_handle, "info config");
#endif
capturer->EndCapture(); capturer->EndCapture();
info = capturer->GetCapture(); info = capturer->GetCapture();
auto start = info.find("LAMMPS version:"); auto start = info.find("LAMMPS version:");
@ -675,33 +625,18 @@ void LammpsGui::preferences()
void LammpsGui::start_lammps() void LammpsGui::start_lammps()
{ {
char **args = lammps_args.data(); char **args = lammps_args.data();
int nargs = lammps_args.size(); int narg = lammps_args.size();
#if defined(LAMMPS_GUI_USE_PLUGIN) lammps.open(narg, args);
liblammpsplugin_t *lammps = (liblammpsplugin_t *)plugin_handle;
// Create LAMMPS instance if not already present if (lammps.has_error()) {
if (!lammps_handle) lammps_handle = lammps->open_no_mpi(nargs, args, nullptr);
if (lammps->has_error(lammps_handle)) {
constexpr int BUFLEN = 1024; constexpr int BUFLEN = 1024;
char errorbuf[BUFLEN]; char errorbuf[BUFLEN];
lammps->get_last_error_message(lammps_handle, errorbuf, BUFLEN); lammps.get_last_error_message(errorbuf, BUFLEN);
QMessageBox::critical(this, "LAMMPS-GUI Error", QMessageBox::critical(this, "LAMMPS-GUI Error",
QString("Error launching LAMMPS:\n\n") + errorbuf); QString("Error launching LAMMPS:\n\n") + errorbuf);
} }
#else
// Create LAMMPS instance if not already present
if (!lammps_handle) lammps_handle = lammps_open_no_mpi(nargs, args, nullptr);
if (lammps_has_error(lammps_handle)) {
constexpr int BUFLEN = 1024;
char errorbuf[BUFLEN];
lammps_get_last_error_message(lammps_handle, errorbuf, BUFLEN);
QMessageBox::critical(this, "LAMMPS-GUI Error",
QString("Error launching LAMMPS:\n\n") + errorbuf);
}
#endif
} }
// Local Variables: // Local Variables:

View File

@ -15,9 +15,13 @@
#define LAMMPSGUI_H #define LAMMPSGUI_H
#include <QMainWindow> #include <QMainWindow>
#include <QList>
#include <QString> #include <QString>
#include <vector> #include <vector>
#include "lammpswrapper.h"
// forward declarations // forward declarations
QT_BEGIN_NAMESPACE QT_BEGIN_NAMESPACE
@ -87,13 +91,11 @@ private:
QString current_file; QString current_file;
QString current_dir; QString current_dir;
QString temp_dir; LammpsWrapper lammps;
void *lammps_handle; std::string plugin_path;
void *plugin_handle;
const char *plugin_path;
bool is_running; bool is_running;
int nthreads; int nthreads;
std::vector<char *> recent_files; QList<QString> recent_files;
std::vector<char *> lammps_args; std::vector<char *> lammps_args;
}; };

View File

@ -1,44 +0,0 @@
/* ----------------------------------------------------------------------
LAMMPS - Large-scale Atomic/Molecular Massively Parallel Simulator
https://www.lammps.org/, Sandia National Laboratories
LAMMPS development team: developers@lammps.org
Copyright (2003) Sandia Corporation. Under the terms of Contract
DE-AC04-94AL85000 with Sandia Corporation, the U.S. Government retains
certain rights in this software. This software is distributed under
the GNU General Public License.
See the README file in the top-level LAMMPS directory.
------------------------------------------------------------------------- */
#include "lammpsrunner.h"
#if defined(LAMMPS_GUI_USE_PLUGIN)
#include "liblammpsplugin.h"
#else
#include "library.h"
#endif
#include <cstdio>
LammpsRunner::LammpsRunner(QObject *parent) :
QThread(parent), handle(nullptr), plugin(nullptr), input(nullptr)
{
}
void LammpsRunner::run()
{
if (handle) {
#if defined(LAMMPS_GUI_USE_PLUGIN)
liblammpsplugin_t *lammps = (liblammpsplugin_t *)plugin;
lammps->commands_string(handle, input);
#else
lammps_commands_string(handle, input);
#endif
}
emit resultReady();
}
// Local Variables:
// c-basic-offset: 4
// End:

View File

@ -20,17 +20,21 @@ class LammpsRunner : public QThread {
Q_OBJECT Q_OBJECT
public: public:
LammpsRunner(QObject *parent = nullptr); LammpsRunner(QObject *parent = nullptr) : QThread(parent), lammps(nullptr), input(nullptr) {}
~LammpsRunner() = default; ~LammpsRunner() = default;
// execute LAMMPS run in runner thread public:
void run() override; // execute LAMMPS in runner thread
void run() override
{
lammps->commands_string(input);
emit resultReady();
}
// transfer info to worker thread // transfer info to worker thread
void setup_run(void *_handle, const char *_input, void *_plugin = nullptr) void setup_run(LammpsWrapper *_lammps, const char *_input)
{ {
handle = _handle; lammps = _lammps;
plugin = _plugin;
input = _input; input = _input;
} }
@ -38,7 +42,7 @@ signals:
void resultReady(); void resultReady();
private: private:
void *handle, *plugin; LammpsWrapper *lammps;
const char *input; const char *input;
}; };

View File

@ -0,0 +1,185 @@
/* ----------------------------------------------------------------------
LAMMPS - Large-scale Atomic/Molecular Massively Parallel Simulator
https://www.lammps.org/, Sandia National Laboratories
LAMMPS development team: developers@lammps.org
Copyright (2003) Sandia Corporation. Under the terms of Contract
DE-AC04-94AL85000 with Sandia Corporation, the U.S. Government retains
certain rights in this software. This software is distributed under
the GNU General Public License.
See the README file in the top-level LAMMPS directory.
------------------------------------------------------------------------- */
#include "lammpswrapper.h"
#if defined(LAMMPS_GUI_USE_PLUGIN)
#include "liblammpsplugin.h"
#else
#include "library.h"
#endif
LammpsWrapper::LammpsWrapper() :
lammps_handle(nullptr), plugin_handle(nullptr)
{
}
void LammpsWrapper::open(int narg, char **args)
{
if (!lammps_handle) {
#if defined(LAMMPS_GUI_USE_PLUGIN)
lammps_handle = ((liblammpsplugin_t *)plugin_handle)->open_no_mpi(narg, args, nullptr);
#else
lammps_handle = lammps_open_no_mpi(narg, args, nullptr);
#endif
}
}
int LammpsWrapper::extract_setting(const char *keyword)
{
int val = 0;
if (lammps_handle) {
#if defined(LAMMPS_GUI_USE_PLUGIN)
val = ((liblammpsplugin_t *)plugin_handle)->extract_setting(lammps_handle, keyword);
#else
val = lammps_extract_setting(lammps_handle, keyword);
#endif
}
return val;
}
double LammpsWrapper::get_thermo(const char *keyword)
{
double val = 0.0;
if (lammps_handle) {
#if defined(LAMMPS_GUI_USE_PLUGIN)
val = ((liblammpsplugin_t *)plugin_handle)->get_thermo(lammps_handle, keyword);
#else
val = lammps_get_thermo(lammps_handle, keyword);
#endif
}
return val;
}
bool LammpsWrapper::is_running()
{
int val = 0;
if (lammps_handle) {
#if defined(LAMMPS_GUI_USE_PLUGIN)
val = ((liblammpsplugin_t *)plugin_handle)->is_running(lammps_handle);
#else
val = lammps_is_running(lammps_handle);
#endif
}
return val != 0;
}
void LammpsWrapper::command(const char *input)
{
if (lammps_handle) {
#if defined(LAMMPS_GUI_USE_PLUGIN)
((liblammpsplugin_t *)plugin_handle)->command(lammps_handle, input);
#else
lammps_command(lammps_handle, input);
#endif
}
}
void LammpsWrapper::commands_string(const char *input)
{
if (lammps_handle) {
#if defined(LAMMPS_GUI_USE_PLUGIN)
((liblammpsplugin_t *)plugin_handle)->commands_string(lammps_handle, input);
#else
lammps_commands_string(lammps_handle, input);
#endif
}
}
// may be called with null handle. returns global error then.
bool LammpsWrapper::has_error() const
{
#if defined(LAMMPS_GUI_USE_PLUGIN)
return ((liblammpsplugin_t *)plugin_handle)->has_error(lammps_handle) != 0;
#else
return lammps_has_error(lammps_handle) != 0;
#endif
}
int LammpsWrapper::get_last_error_message(char *buf, int buflen)
{
#if defined(LAMMPS_GUI_USE_PLUGIN)
return ((liblammpsplugin_t *)plugin_handle)->get_last_error_message(lammps_handle, buf, buflen);
#else
return lammps_get_last_error_message(lammps_handle, buf, buflen);
#endif
}
void LammpsWrapper::force_timeout()
{
#if defined(LAMMPS_GUI_USE_PLUGIN)
((liblammpsplugin_t *)plugin_handle)->force_timeout(lammps_handle);
#else
lammps_force_timeout(lammps_handle);
#endif
}
void LammpsWrapper::close()
{
#if defined(LAMMPS_GUI_USE_PLUGIN)
if (lammps_handle) ((liblammpsplugin_t *)plugin_handle)->close(lammps_handle);
#else
if (lammps_handle) lammps_close(lammps_handle);
#endif
lammps_handle = nullptr;
}
void LammpsWrapper::finalize()
{
#if defined(LAMMPS_GUI_USE_PLUGIN)
if (lammps_handle) {
liblammpsplugin_t *lammps = (liblammpsplugin_t *)plugin_handle;
lammps->close(lammps_handle);
lammps->mpi_finalize();
lammps->kokkos_finalize();
lammps->python_finalize();
}
#else
if (lammps_handle) {
lammps_close(lammps_handle);
lammps_mpi_finalize();
lammps_kokkos_finalize();
lammps_python_finalize();
}
#endif
}
#if defined(LAMMPS_GUI_USE_PLUGIN)
bool LammpsWrapper::has_plugin() const
{
return true;
}
bool LammpsWrapper::load_lib(const char *libfile)
{
if (plugin_handle) liblammpsplugin_release((liblammpsplugin_t *)plugin_handle);
plugin_handle = liblammpsplugin_load(libfile);
if (!plugin_handle) return false;
if (((liblammpsplugin_t *)plugin_handle)->abiversion != LAMMPSPLUGIN_ABI_VERSION) return false;
return true;
}
#else
bool LammpsWrapper::has_plugin() const
{
return false;
}
bool LammpsWrapper::load_lib(const char *)
{
return true;
}
#endif
// Local Variables:
// c-basic-offset: 4
// End:

View File

@ -0,0 +1,49 @@
/* -*- c++ -*- ----------------------------------------------------------
LAMMPS - Large-scale Atomic/Molecular Massively Parallel Simulator
https://www.lammps.org/, Sandia National Laboratories
LAMMPS development team: developers@lammps.org
Copyright (2003) Sandia Corporation. Under the terms of Contract
DE-AC04-94AL85000 with Sandia Corporation, the U.S. Government retains
certain rights in this software. This software is distributed under
the GNU General Public License.
See the README file in the top-level LAMMPS directory.
------------------------------------------------------------------------- */
#ifndef LAMMPSWRAPPER_H
#define LAMMPSWRAPPER_H
class LammpsWrapper {
public:
LammpsWrapper();
public:
void open(int nargs, char **args);
void close();
void finalize();
void command(const char *);
void commands_string(const char *);
void force_timeout();
int extract_setting(const char *keyword);
double get_thermo(const char *keyword);
bool is_open() const { return lammps_handle != nullptr; }
bool is_running();
bool has_error() const;
int get_last_error_message(char *errorbuf, int buflen);
bool load_lib(const char *lammpslib);
bool has_plugin() const;
private:
void *lammps_handle;
void *plugin_handle;
};
#endif
// Local Variables:
// c-basic-offset: 4
// End: