import test infrastructure for c, c++ and python library usage

This commit is contained in:
Axel Kohlmeyer 2020-08-25 11:03:31 -04:00
parent dc241abb85
commit 69cffb2d04
No known key found for this signature in database
GPG Key ID: D9B44E93BF0C375A
11 changed files with 1010 additions and 0 deletions

View File

@ -3,6 +3,9 @@ include(GTest)
add_subdirectory(utils)
add_subdirectory(formats)
add_subdirectory(commands)
add_subdirectory(c-library)
add_subdirectory(cplusplus)
add_subdirectory(python)
add_subdirectory(force-styles)
find_package(ClangFormat 8.0)

View File

@ -0,0 +1,13 @@
add_executable(library-open library-open.cpp)
target_link_libraries(library-open PRIVATE lammps GTest::GTest GTest::GTestMain)
add_test(LibraryOpen library-open)
add_executable(library-commands library-commands.cpp)
target_link_libraries(library-commands PRIVATE lammps GTest::GTest GTest::GTestMain)
add_test(LibraryCommands library-commands)
add_executable(library-properties library-properties.cpp)
target_link_libraries(library-properties PRIVATE lammps GTest::GTest GTest::GTestMain)
add_test(LibraryProperties library-properties)

View File

@ -0,0 +1,103 @@
// unit tests for issuing command to a LAMMPS instance through the library interface
#include "library.h"
#include "lammps.h"
#include <string>
#include "gtest/gtest.h"
const char *demo_input[] = {
"region box block 0 $x 0 2 0 2",
"create_box 1 box",
"create_atoms 1 single 1.0 1.0 ${zpos}" };
const char *cont_input[] = {
"create_atoms 1 single &",
"0.2 0.1 0.1" };
class LAMMPS_commands : public ::testing::Test
{
protected:
void *lmp;
LAMMPS_commands() {};
~LAMMPS_commands() override {};
void SetUp() override {
const char *args[] = {"LAMMPS_test",
"-log", "none",
"-echo", "screen",
"-nocite", "-var","x","2",
"-var", "zpos", "1.5"};
char **argv = (char **)args;
int argc = sizeof(args)/sizeof(char *);
::testing::internal::CaptureStdout();
lmp = lammps_open_no_mpi(argc, argv, NULL);
std::string output = ::testing::internal::GetCapturedStdout();
EXPECT_STREQ(output.substr(0,8).c_str(), "LAMMPS (");
}
void TearDown() override {
::testing::internal::CaptureStdout();
lammps_close(lmp);
std::string output = ::testing::internal::GetCapturedStdout();
EXPECT_STREQ(output.substr(0,16).c_str(), "Total wall time:");
lmp = nullptr;
}
};
TEST_F(LAMMPS_commands, from_file) {
FILE *fp;
const char demo_file[] = "in.test";
const char cont_file[] = "in.cont";
fp = fopen(demo_file,"w");
for (unsigned int i=0; i < sizeof(demo_input)/sizeof(char *); ++i) {
fputs(demo_input[i],fp);
fputc('\n',fp);
}
fclose(fp);
fp = fopen(cont_file,"w");
for (unsigned int i=0; i < sizeof(cont_input)/sizeof(char *); ++i) {
fputs(cont_input[i],fp);
fputc('\n',fp);
}
fclose(fp);
EXPECT_EQ(lammps_get_natoms(lmp),0);
lammps_file(lmp,demo_file);
lammps_file(lmp,cont_file);
EXPECT_EQ(lammps_get_natoms(lmp),2);
unlink(demo_file);
unlink(cont_file);
};
TEST_F(LAMMPS_commands, from_line) {
EXPECT_EQ(lammps_get_natoms(lmp),0);
for (unsigned int i=0; i < sizeof(demo_input)/sizeof(char *); ++i) {
lammps_command(lmp,demo_input[i]);
}
EXPECT_EQ(lammps_get_natoms(lmp),1);
};
TEST_F(LAMMPS_commands, from_list) {
EXPECT_EQ(lammps_get_natoms(lmp),0);
lammps_commands_list(lmp,sizeof(demo_input)/sizeof(char *),demo_input);
lammps_commands_list(lmp,sizeof(cont_input)/sizeof(char *),cont_input);
EXPECT_EQ(lammps_get_natoms(lmp),2);
};
TEST_F(LAMMPS_commands, from_string) {
std::string cmds("");
for (unsigned int i=0; i < sizeof(demo_input)/sizeof(char *); ++i) {
cmds += demo_input[i];
cmds += "\n";
}
for (unsigned int i=0; i < sizeof(cont_input)/sizeof(char *); ++i) {
cmds += cont_input[i];
cmds += "\n";
}
EXPECT_EQ(lammps_get_natoms(lmp),0);
lammps_commands_string(lmp,cmds.c_str());
EXPECT_EQ(lammps_get_natoms(lmp),2);
};

View File

@ -0,0 +1,186 @@
// unit tests for the LAMMPS base class
#include "library.h"
#include "lammps.h"
#include <mpi.h>
#include <cstdio> // for stdin, stdout
#include <string>
#include "gtest/gtest.h"
TEST(lammps_open, null_args) {
::testing::internal::CaptureStdout();
void *handle = lammps_open(0,NULL, MPI_COMM_WORLD, NULL);
std::string output = ::testing::internal::GetCapturedStdout();
EXPECT_STREQ(output.substr(0,6).c_str(),"LAMMPS");
int mpi_init=0;
MPI_Initialized(&mpi_init);
EXPECT_GT(mpi_init,0);
LAMMPS_NS::LAMMPS *lmp = (LAMMPS_NS::LAMMPS *)handle;
EXPECT_EQ(lmp->world, MPI_COMM_WORLD);
EXPECT_EQ(lmp->infile, stdin);
EXPECT_EQ(lmp->screen, stdout);
EXPECT_NE(lmp->citeme, nullptr);
::testing::internal::CaptureStdout();
lammps_close(handle);
output = ::testing::internal::GetCapturedStdout();
EXPECT_STREQ(output.substr(0,16).c_str(), "Total wall time:");
}
TEST(lammps_open, with_args) {
const char *args[] = {"liblammps",
"-log", "none",
"-nocite"};
char **argv = (char **)args;
int argc = sizeof(args)/sizeof(char *);
// MPI is already initialized
MPI_Comm mycomm;
MPI_Comm_split(MPI_COMM_WORLD, 0, 1, &mycomm);
::testing::internal::CaptureStdout();
void *alt_ptr;
void *handle = lammps_open(argc, argv, mycomm, &alt_ptr);
std::string output = ::testing::internal::GetCapturedStdout();
EXPECT_STREQ(output.substr(0,6).c_str(),"LAMMPS");
EXPECT_EQ(handle,alt_ptr);
LAMMPS_NS::LAMMPS *lmp = (LAMMPS_NS::LAMMPS *)handle;
// MPI STUBS uses no real communicators
#if !defined(MPI_STUBS)
EXPECT_NE(lmp->world, MPI_COMM_WORLD);
#endif
EXPECT_EQ(lmp->world, mycomm);
EXPECT_EQ(lmp->infile, stdin);
EXPECT_EQ(lmp->logfile, nullptr);
EXPECT_EQ(lmp->citeme, nullptr);
EXPECT_EQ(lmp->kokkos, nullptr);
EXPECT_EQ(lmp->atomKK, nullptr);
EXPECT_EQ(lmp->memoryKK, nullptr);
::testing::internal::CaptureStdout();
lammps_close(handle);
output = ::testing::internal::GetCapturedStdout();
EXPECT_STREQ(output.substr(0,16).c_str(), "Total wall time:");
}
TEST(lammps_open, with_kokkos) {
if (!LAMMPS_NS::LAMMPS::is_installed_pkg("KOKKOS")) GTEST_SKIP();
const char *args[] = {"liblammps",
"-k", "on", "t", "2",
"-sf", "kk",
"-log", "none" };
char **argv = (char **)args;
int argc = sizeof(args)/sizeof(char *);
::testing::internal::CaptureStdout();
void *alt_ptr;
void *handle = lammps_open(argc, argv, MPI_COMM_WORLD, &alt_ptr);
std::string output = ::testing::internal::GetCapturedStdout();
EXPECT_STREQ(output.substr(0,6).c_str(),"LAMMPS");
EXPECT_EQ(handle,alt_ptr);
LAMMPS_NS::LAMMPS *lmp = (LAMMPS_NS::LAMMPS *)handle;
EXPECT_EQ(lmp->world, MPI_COMM_WORLD);
EXPECT_EQ(lmp->infile, stdin);
EXPECT_EQ(lmp->logfile, nullptr);
EXPECT_NE(lmp->citeme, nullptr);
EXPECT_EQ(lmp->num_package, 0);
EXPECT_NE(lmp->kokkos, nullptr);
EXPECT_NE(lmp->atomKK, nullptr);
EXPECT_NE(lmp->memoryKK, nullptr);
::testing::internal::CaptureStdout();
lammps_close(handle);
output = ::testing::internal::GetCapturedStdout();
EXPECT_STREQ(output.substr(0,16).c_str(), "Total wall time:");
}
TEST(lammps_open_no_mpi, no_screen) {
const char *args[] = {"liblammps",
"-log", "none",
"-screen", "none",
"-nocite"};
char **argv = (char **)args;
int argc = sizeof(args)/sizeof(char *);
::testing::internal::CaptureStdout();
void *alt_ptr;
void *handle = lammps_open_no_mpi(argc, argv, &alt_ptr);
std::string output = ::testing::internal::GetCapturedStdout();
EXPECT_STREQ(output.c_str(),"");
EXPECT_EQ(handle,alt_ptr);
LAMMPS_NS::LAMMPS *lmp = (LAMMPS_NS::LAMMPS *)handle;
EXPECT_EQ(lmp->world, MPI_COMM_WORLD);
EXPECT_EQ(lmp->infile, stdin);
EXPECT_EQ(lmp->screen, nullptr);
EXPECT_EQ(lmp->logfile, nullptr);
EXPECT_EQ(lmp->citeme, nullptr);
EXPECT_EQ(lmp->suffix_enable, 0);
EXPECT_STREQ(lmp->exename, "liblammps");
EXPECT_EQ(lmp->num_package, 0);
::testing::internal::CaptureStdout();
lammps_close(handle);
output = ::testing::internal::GetCapturedStdout();
EXPECT_STREQ(output.c_str(), "");
}
TEST(lammps_open_no_mpi, with_omp) {
if (!LAMMPS_NS::LAMMPS::is_installed_pkg("USER-OMP")) GTEST_SKIP();
const char *args[] = {"liblammps",
"-pk", "omp", "2", "neigh", "no",
"-sf", "omp",
"-log", "none",
"-nocite"};
char **argv = (char **)args;
int argc = sizeof(args)/sizeof(char *);
::testing::internal::CaptureStdout();
void *alt_ptr;
void *handle = lammps_open_no_mpi(argc, argv, &alt_ptr);
std::string output = ::testing::internal::GetCapturedStdout();
EXPECT_STREQ(output.substr(0,6).c_str(),"LAMMPS");
EXPECT_EQ(handle,alt_ptr);
LAMMPS_NS::LAMMPS *lmp = (LAMMPS_NS::LAMMPS *)handle;
EXPECT_EQ(lmp->world, MPI_COMM_WORLD);
EXPECT_EQ(lmp->infile, stdin);
EXPECT_EQ(lmp->logfile, nullptr);
EXPECT_EQ(lmp->citeme, nullptr);
EXPECT_EQ(lmp->suffix_enable, 1);
EXPECT_STREQ(lmp->suffix, "omp");
EXPECT_EQ(lmp->suffix2, nullptr);
EXPECT_STREQ(lmp->exename, "liblammps");
EXPECT_EQ(lmp->num_package, 1);
::testing::internal::CaptureStdout();
lammps_close(handle);
output = ::testing::internal::GetCapturedStdout();
EXPECT_STREQ(output.substr(0,16).c_str(), "Total wall time:");
}
TEST(lammps_open_fortran, no_args) {
// MPI is already initialized
MPI_Comm mycomm;
MPI_Comm_split(MPI_COMM_WORLD, 0, 1, &mycomm);
int fcomm = MPI_Comm_c2f(mycomm);
::testing::internal::CaptureStdout();
void *handle = lammps_open_fortran(0, NULL, fcomm, NULL);
std::string output = ::testing::internal::GetCapturedStdout();
EXPECT_STREQ(output.substr(0,6).c_str(),"LAMMPS");
LAMMPS_NS::LAMMPS *lmp = (LAMMPS_NS::LAMMPS *)handle;
// MPI STUBS uses no real communicators
#if !defined(MPI_STUBS)
EXPECT_NE(lmp->world, MPI_COMM_WORLD);
#endif
EXPECT_EQ(lmp->world, mycomm);
EXPECT_EQ(lmp->infile, stdin);
EXPECT_EQ(lmp->screen, stdout);
EXPECT_NE(lmp->logfile, nullptr);
EXPECT_NE(lmp->citeme, nullptr);
::testing::internal::CaptureStdout();
lammps_close(handle);
output = ::testing::internal::GetCapturedStdout();
EXPECT_STREQ(output.substr(0,16).c_str(), "Total wall time:");
}

View File

@ -0,0 +1,45 @@
// unit tests for checking and changing simulation properties through the library interface
#include "library.h"
#include "lammps.h"
#include <string>
#include "gtest/gtest.h"
const char *demo_input[] = {
"region box block 0 $x 0 2 0 2",
"create_box 1 box",
"create_atoms 1 single 1.0 1.0 ${zpos}" };
const char *cont_input[] = {
"create_atoms 1 single &",
"0.2 0.1 0.1" };
class LAMMPS_properties : public ::testing::Test
{
protected:
void *lmp;
LAMMPS_properties() {};
~LAMMPS_properties() override {};
void SetUp() override {
const char *args[] = {"LAMMPS_test", "-log", "none",
"-echo", "screen", "-nocite" };
char **argv = (char **)args;
int argc = sizeof(args)/sizeof(char *);
::testing::internal::CaptureStdout();
lmp = lammps_open_no_mpi(argc, argv, NULL);
std::string output = ::testing::internal::GetCapturedStdout();
EXPECT_STREQ(output.substr(0,8).c_str(), "LAMMPS (");
}
void TearDown() override {
::testing::internal::CaptureStdout();
lammps_close(lmp);
std::string output = ::testing::internal::GetCapturedStdout();
EXPECT_STREQ(output.substr(0,16).c_str(), "Total wall time:");
lmp = nullptr;
}
};
TEST_F(LAMMPS_properties, box) {
};

View File

@ -0,0 +1,8 @@
add_executable(lammps-class lammps-class.cpp)
target_link_libraries(lammps-class PRIVATE lammps GTest::GMockMain GTest::GTest GTest::GMock)
add_test(LammpsClass lammps-class)
add_executable(input-class input-class.cpp)
target_link_libraries(input-class PRIVATE lammps GTest::GTest GTest::GTestMain)
add_test(InputClass input-class)

View File

@ -0,0 +1,114 @@
// unit tests for issuing command to a LAMMPS instance through the Input class
#include "lammps.h"
#include "input.h"
#include "atom.h"
#include "memory.h"
#include <mpi.h>
#include <string>
#include <cstring>
#include "gtest/gtest.h"
const char *demo_input[] = {
"region box block 0 $x 0 2 0 2",
"create_box 1 box",
"create_atoms 1 single 1.0 1.0 ${zpos}" };
const char *cont_input[] = {
"create_atoms 1 single &",
"0.2 0.1 0.1" };
namespace LAMMPS_NS
{
class Input_commands : public ::testing::Test
{
protected:
LAMMPS *lmp;
Input_commands() {
const char *args[] = {"LAMMPS_test"};
char **argv = (char **)args;
int argc = sizeof(args)/sizeof(char *);
int flag;
MPI_Initialized(&flag);
if (!flag) MPI_Init(&argc,&argv);
}
~Input_commands() override {}
void SetUp() override {
const char *args[] = {"LAMMPS_test",
"-log", "none",
"-echo", "screen",
"-nocite",
"-var", "zpos", "1.5",
"-var","x","2"};
char **argv = (char **)args;
int argc = sizeof(args)/sizeof(char *);
::testing::internal::CaptureStdout();
lmp = new LAMMPS(argc, argv, MPI_COMM_WORLD);
std::string output = ::testing::internal::GetCapturedStdout();
EXPECT_STREQ(output.substr(0,8).c_str(), "LAMMPS (");
}
void TearDown() override {
::testing::internal::CaptureStdout();
delete lmp;
std::string output = ::testing::internal::GetCapturedStdout();
EXPECT_STREQ(output.substr(0,16).c_str(), "Total wall time:");
lmp = nullptr;
}
};
TEST_F(Input_commands, from_file) {
FILE *fp;
const char demo_file[] = "in.test";
const char cont_file[] = "in.cont";
fp = fopen(demo_file,"w");
for (unsigned int i=0; i < sizeof(demo_input)/sizeof(char *); ++i) {
fputs(demo_input[i],fp);
fputc('\n',fp);
}
fclose(fp);
fp = fopen(cont_file,"w");
for (unsigned int i=0; i < sizeof(cont_input)/sizeof(char *); ++i) {
fputs(cont_input[i],fp);
fputc('\n',fp);
}
fclose(fp);
EXPECT_EQ(lmp->atom->natoms,0);
lmp->input->file(demo_file);
lmp->input->file(cont_file);
EXPECT_EQ(lmp->atom->natoms,2);
unlink(demo_file);
unlink(cont_file);
};
TEST_F(Input_commands, from_line) {
EXPECT_EQ(lmp->atom->natoms,0);
for (unsigned int i=0; i < sizeof(demo_input)/sizeof(char *); ++i) {
lmp->input->one(demo_input[i]);
}
EXPECT_EQ(lmp->atom->natoms,1);
};
TEST_F(Input_commands, substitute) {
char *string,*scratch;
int nstring=100,nscratch=100;
lmp->memory->create(string,nstring,"test:string");
lmp->memory->create(scratch,nscratch,"test:scratch");
strcpy(string,demo_input[0]);
lmp->input->substitute(string,scratch,nstring,nscratch,0);
EXPECT_STREQ(string,"region box block 0 2 0 2 0 2");
strcpy(string,demo_input[2]);
lmp->input->substitute(string,scratch,nstring,nscratch,0);
EXPECT_STREQ(string,"create_atoms 1 single 1.0 1.0 1.5");
lmp->memory->destroy(string);
lmp->memory->destroy(scratch);
};
}

View File

@ -0,0 +1,351 @@
// unit tests for the LAMMPS base class
#include "lammps.h"
#include <mpi.h>
#include <cstdio> // for stdin, stdout
#include <string>
#include "gmock/gmock.h"
#include "gtest/gtest.h"
using ::testing::StartsWith;
namespace LAMMPS_NS
{
// test fixture for regular tests
class LAMMPS_plain : public ::testing::Test {
protected:
LAMMPS *lmp;
LAMMPS_plain() : lmp(nullptr) {
const char *args[] = {"LAMMPS_test"};
char **argv = (char **)args;
int argc = sizeof(args)/sizeof(char *);
int flag;
MPI_Initialized(&flag);
if (!flag) MPI_Init(&argc,&argv);
}
~LAMMPS_plain() override {
lmp = nullptr;
}
void SetUp() override {
const char *args[] = {"LAMMPS_test",
"-log", "none",
"-echo", "both",
"-nocite"};
char **argv = (char **)args;
int argc = sizeof(args)/sizeof(char *);
::testing::internal::CaptureStdout();
lmp = new LAMMPS(argc, argv, MPI_COMM_WORLD);
std::string output = ::testing::internal::GetCapturedStdout();
EXPECT_THAT(output, StartsWith("LAMMPS ("));
}
void TearDown() override {
::testing::internal::CaptureStdout();
delete lmp;
std::string output = ::testing::internal::GetCapturedStdout();
EXPECT_THAT(output, StartsWith("Total wall time:"));
}
};
TEST_F(LAMMPS_plain, InitMembers)
{
EXPECT_NE(lmp->memory, nullptr);
EXPECT_NE(lmp->error, nullptr);
EXPECT_NE(lmp->universe, nullptr);
EXPECT_NE(lmp->input, nullptr);
EXPECT_NE(lmp->atom, nullptr);
EXPECT_NE(lmp->update, nullptr);
EXPECT_NE(lmp->neighbor, nullptr);
EXPECT_NE(lmp->comm, nullptr);
EXPECT_NE(lmp->domain, nullptr);
EXPECT_NE(lmp->force, nullptr);
EXPECT_NE(lmp->modify, nullptr);
EXPECT_NE(lmp->group, nullptr);
EXPECT_NE(lmp->output, nullptr);
EXPECT_NE(lmp->timer, nullptr);
EXPECT_EQ(lmp->world, MPI_COMM_WORLD);
EXPECT_EQ(lmp->infile, stdin);
EXPECT_EQ(lmp->screen, stdout);
EXPECT_EQ(lmp->logfile, nullptr);
EXPECT_GE(lmp->initclock, 0.0);
EXPECT_EQ(lmp->suffix_enable, 0);
EXPECT_EQ(lmp->suffix, nullptr);
EXPECT_EQ(lmp->suffix2, nullptr);
EXPECT_STREQ(lmp->exename, "LAMMPS_test");
EXPECT_EQ(lmp->num_package, 0);
EXPECT_EQ(lmp->clientserver, 0);
EXPECT_EQ(lmp->kokkos, nullptr);
EXPECT_EQ(lmp->atomKK, nullptr);
EXPECT_EQ(lmp->memoryKK, nullptr);
EXPECT_NE(lmp->python, nullptr);
EXPECT_EQ(lmp->citeme, nullptr);
if (LAMMPS::has_git_info) {
EXPECT_STRNE(LAMMPS::git_commit,"");
EXPECT_STRNE(LAMMPS::git_branch,"");
EXPECT_STRNE(LAMMPS::git_descriptor,"");
} else {
EXPECT_STREQ(LAMMPS::git_commit,"(unknown)");
EXPECT_STREQ(LAMMPS::git_branch,"(unknown)");
EXPECT_STREQ(LAMMPS::git_descriptor,"(unknown)");
}
}
TEST_F(LAMMPS_plain, TestStyles)
{
// skip tests if base class is not available
if (lmp == nullptr) return;
const char *found;
const char *atom_styles[] = {
"atomic", "body", "charge", "ellipsoid", "hybrid",
"line", "sphere", "tri", NULL };
for (int i = 0; atom_styles[i] != NULL; ++i) {
found = lmp->match_style("atom",atom_styles[i]);
EXPECT_STREQ(found, NULL);
}
const char *molecule_atom_styles[] = {
"angle", "bond", "full", "molecular", "template", NULL };
for (int i = 0; molecule_atom_styles[i] != NULL; ++i) {
found = lmp->match_style("atom",molecule_atom_styles[i]);
EXPECT_STREQ(found, "MOLECULE");
}
const char *kokkos_atom_styles[] = {
"angle/kk", "bond/kk", "full/kk", "molecular/kk", "hybrid/kk", NULL };
for (int i = 0; kokkos_atom_styles[i] != NULL; ++i) {
found = lmp->match_style("atom",kokkos_atom_styles[i]);
EXPECT_STREQ(found, "KOKKOS");
}
found = lmp->match_style("atom","dipole");
EXPECT_STREQ(found,"DIPOLE");
found = lmp->match_style("atom","peri");
EXPECT_STREQ(found,"PERI");
found = lmp->match_style("atom","spin");
EXPECT_STREQ(found,"SPIN");
found = lmp->match_style("atom","wavepacket");
EXPECT_STREQ(found,"USER-AWPMD");
found = lmp->match_style("atom","dpd");
EXPECT_STREQ(found,"USER-DPD");
found = lmp->match_style("atom","edpd");
EXPECT_STREQ(found,"USER-MESODPD");
found = lmp->match_style("atom","mdpd");
EXPECT_STREQ(found,"USER-MESODPD");
found = lmp->match_style("atom","tdpd");
EXPECT_STREQ(found,"USER-MESODPD");
found = lmp->match_style("atom","spin");
EXPECT_STREQ(found,"SPIN");
found = lmp->match_style("atom","smd");
EXPECT_STREQ(found,"USER-SMD");
found = lmp->match_style("atom","sph");
EXPECT_STREQ(found,"USER-SPH");
found = lmp->match_style("atom","i_don't_exist");
EXPECT_STREQ(found,NULL);
}
// test fixture for OpenMP with 2 threads
class LAMMPS_omp : public ::testing::Test {
protected:
LAMMPS *lmp;
LAMMPS_omp() : lmp(nullptr) {
const char *args[] = {"LAMMPS_test"};
char **argv = (char **)args;
int argc = sizeof(args)/sizeof(char *);
int flag;
MPI_Initialized(&flag);
if (!flag) MPI_Init(&argc,&argv);
}
~LAMMPS_omp() override {
lmp = nullptr;
}
void SetUp() override {
const char *args[] = {"LAMMPS_test",
"-log", "none",
"-screen", "none",
"-echo", "screen",
"-pk", "omp","2", "neigh", "yes",
"-sf", "omp"
};
char **argv = (char **)args;
int argc = sizeof(args)/sizeof(char *);
// only run this test fixture with omp suffix if USER-OMP package is installed
if (LAMMPS::is_installed_pkg("USER-OMP"))
lmp = new LAMMPS(argc, argv, MPI_COMM_WORLD);
else GTEST_SKIP();
}
void TearDown() override {
delete lmp;
}
};
TEST_F(LAMMPS_omp, InitMembers)
{
EXPECT_NE(lmp->memory, nullptr);
EXPECT_NE(lmp->error, nullptr);
EXPECT_NE(lmp->universe, nullptr);
EXPECT_NE(lmp->input, nullptr);
EXPECT_NE(lmp->atom, nullptr);
EXPECT_NE(lmp->update, nullptr);
EXPECT_NE(lmp->neighbor, nullptr);
EXPECT_NE(lmp->comm, nullptr);
EXPECT_NE(lmp->domain, nullptr);
EXPECT_NE(lmp->force, nullptr);
EXPECT_NE(lmp->modify, nullptr);
EXPECT_NE(lmp->group, nullptr);
EXPECT_NE(lmp->output, nullptr);
EXPECT_NE(lmp->timer, nullptr);
EXPECT_EQ(lmp->world, MPI_COMM_WORLD);
EXPECT_EQ(lmp->infile, stdin);
EXPECT_EQ(lmp->screen, nullptr);
EXPECT_EQ(lmp->logfile, nullptr);
EXPECT_GE(lmp->initclock, 0.0);
EXPECT_EQ(lmp->suffix_enable, 1);
EXPECT_STREQ(lmp->suffix, "omp");
EXPECT_EQ(lmp->suffix2, nullptr);
EXPECT_STREQ(lmp->exename, "LAMMPS_test");
EXPECT_EQ(lmp->num_package, 1);
EXPECT_EQ(lmp->clientserver, 0);
EXPECT_EQ(lmp->kokkos, nullptr);
EXPECT_EQ(lmp->atomKK, nullptr);
EXPECT_EQ(lmp->memoryKK, nullptr);
EXPECT_NE(lmp->python, nullptr);
EXPECT_NE(lmp->citeme, nullptr);
if (LAMMPS::has_git_info) {
EXPECT_STRNE(LAMMPS::git_commit,"");
EXPECT_STRNE(LAMMPS::git_branch,"");
EXPECT_STRNE(LAMMPS::git_descriptor,"");
} else {
EXPECT_STREQ(LAMMPS::git_commit,"(unknown)");
EXPECT_STREQ(LAMMPS::git_branch,"(unknown)");
EXPECT_STREQ(LAMMPS::git_descriptor,"(unknown)");
}
}
// test fixture for Kokkos tests
class LAMMPS_kokkos : public ::testing::Test {
protected:
LAMMPS *lmp;
LAMMPS_kokkos() : lmp(nullptr) {
const char *args[] = {"LAMMPS_test"};
char **argv = (char **)args;
int argc = sizeof(args)/sizeof(char *);
int flag;
MPI_Initialized(&flag);
if (!flag) MPI_Init(&argc,&argv);
}
~LAMMPS_kokkos() override {
lmp = nullptr;
}
void SetUp() override {
const char *args[] = {"LAMMPS_test",
"-log", "none",
"-echo", "none",
"-screen", "none",
"-k", "on","t", "2",
"-sf", "kk"
};
char **argv = (char **)args;
int argc = sizeof(args)/sizeof(char *);
// only run this test fixture with kk suffix if KOKKOS package is installed
// also need to figure out a way to find which parallelizations are enabled
if (LAMMPS::is_installed_pkg("KOKKOS")) {
::testing::internal::CaptureStdout();
lmp = new LAMMPS(argc, argv, MPI_COMM_WORLD);
std::string output = ::testing::internal::GetCapturedStdout();
EXPECT_THAT(output, StartsWith("Kokkos::OpenMP::"));
} else GTEST_SKIP();
}
void TearDown() override {
delete lmp;
}
};
TEST_F(LAMMPS_kokkos, InitMembers)
{
EXPECT_NE(lmp->memory, nullptr);
EXPECT_NE(lmp->error, nullptr);
EXPECT_NE(lmp->universe, nullptr);
EXPECT_NE(lmp->input, nullptr);
EXPECT_NE(lmp->atom, nullptr);
EXPECT_NE(lmp->update, nullptr);
EXPECT_NE(lmp->neighbor, nullptr);
EXPECT_NE(lmp->comm, nullptr);
EXPECT_NE(lmp->domain, nullptr);
EXPECT_NE(lmp->force, nullptr);
EXPECT_NE(lmp->modify, nullptr);
EXPECT_NE(lmp->group, nullptr);
EXPECT_NE(lmp->output, nullptr);
EXPECT_NE(lmp->timer, nullptr);
EXPECT_EQ(lmp->world, MPI_COMM_WORLD);
EXPECT_EQ(lmp->infile, stdin);
EXPECT_EQ(lmp->screen, nullptr);
EXPECT_EQ(lmp->logfile, nullptr);
EXPECT_GE(lmp->initclock, 0.0);
EXPECT_EQ(lmp->suffix_enable, 1);
EXPECT_STREQ(lmp->suffix, "kk");
EXPECT_EQ(lmp->suffix2, nullptr);
EXPECT_STREQ(lmp->exename, "LAMMPS_test");
EXPECT_EQ(lmp->num_package, 0);
EXPECT_EQ(lmp->clientserver, 0);
EXPECT_NE(lmp->kokkos, nullptr);
EXPECT_NE(lmp->atomKK, nullptr);
EXPECT_NE(lmp->memoryKK, nullptr);
EXPECT_NE(lmp->python, nullptr);
EXPECT_NE(lmp->citeme, nullptr);
if (LAMMPS::has_git_info) {
EXPECT_STRNE(LAMMPS::git_commit,"");
EXPECT_STRNE(LAMMPS::git_branch,"");
EXPECT_STRNE(LAMMPS::git_descriptor,"");
} else {
EXPECT_STREQ(LAMMPS::git_commit,"(unknown)");
EXPECT_STREQ(LAMMPS::git_branch,"(unknown)");
EXPECT_STREQ(LAMMPS::git_descriptor,"(unknown)");
}
}
// check help message printing
TEST(LAMMPS_help, HelpMessage) {
const char *args[] = {"LAMMPS_test", "-h"};
char **argv = (char **)args;
int argc = sizeof(args)/sizeof(char *);
::testing::internal::CaptureStdout();
LAMMPS *lmp = new LAMMPS(argc, argv, MPI_COMM_WORLD);
std::string output = ::testing::internal::GetCapturedStdout();
EXPECT_THAT(output,
StartsWith("\nLarge-scale Atomic/Molecular Massively Parallel Simulator -"));
delete lmp;
}
}

View File

@ -0,0 +1,40 @@
# we must have shared libraries enabled for testing the python module
if(NOT BUILD_SHARED_LIBS)
message(STATUS "Skipping Tests for the LAMMPS Python Module: must enable BUILD_SHARED_LIBS")
return()
endif()
if(CMAKE_VERSION VERSION_LESS 3.12)
find_package(PythonInterp 3.5) # Deprecated since version 3.12
if(PYTHONINTERP_FOUND)
set(Python_EXECUTABLE ${PYTHON_EXECUTABLE})
endif()
else()
find_package(Python3 COMPONENTS Interpreter)
endif()
if (Python_EXECUTABLE)
# prepare to augment the environment so that the LAMMPS python module and the shared library is found.
set(PYTHON_TEST_ENVIRONMENT PYTHONPATH=${LAMMPS_PYTHON_DIR}:$ENV{PYTHONPATH})
if(APPLE)
list(APPEND PYTHON_TEST_ENVIRONMENT DYLD_LIBRARY_PATH=${CMAKE_BINARY_DIR}:$ENV{DYLD_LIBRARY_PATH})
else()
list(APPEND PYTHON_TEST_ENVIRONMENT LD_LIBRARY_PATH=${CMAKE_BINARY_DIR}:$ENV{LD_LIBRARY_PATH})
endif()
if(LAMMPS_MACHINE)
# convert from '_machine' to 'machine'
string(SUBSTRING ${LAMMPS_MACHINE} 1 -1 LAMMPS_MACHINE_NAME)
list(APPEND PYTHON_TEST_ENVIRONMENT LAMMPS_MACHINE_NAME=${LAMMPS_MACHINE_NAME})
endif()
add_test(NAME PythonOpen
COMMAND ${Python_EXECUTABLE} ${CMAKE_CURRENT_SOURCE_DIR}/python-open.py -v
WORKING_DIRECTORY ${EXECUTABLE_OUTPUT_PATH})
set_tests_properties(PythonOpen PROPERTIES ENVIRONMENT "${PYTHON_TEST_ENVIRONMENT}")
add_test(NAME PythonCommands
COMMAND ${Python_EXECUTABLE} ${CMAKE_CURRENT_SOURCE_DIR}/python-commands.py -v
WORKING_DIRECTORY ${EXECUTABLE_OUTPUT_PATH})
set_tests_properties(PythonCommands PROPERTIES ENVIRONMENT "${PYTHON_TEST_ENVIRONMENT}")
else()
message(STATUS "Skipping Tests for the LAMMPS Python Module: no suitable Python interpreter")
endif()

View File

@ -0,0 +1,90 @@
import sys,os,unittest
from lammps import lammps
class PythonCommand(unittest.TestCase):
def setUp(self):
machine=None
if 'LAMMPS_MACHINE_NAME' in os.environ:
machine=os.environ['LAMMPS_MACHINE_NAME']
self.lmp=lammps(name=machine,
cmdargs=['-nocite',
'-log','none',
'-echo','screen',
'-var','zpos','1.5',
'-var','x','2'])
# create demo input strings and files
# a few commands to set up a box with a single atom
self.demo_input="""
region box block 0 $x 0 2 0 2
create_box 1 box
create_atoms 1 single 1.0 1.0 ${zpos}
"""
# another command to add an atom and use a continuation line
self.cont_input="""
create_atoms 1 single &
0.2 0.1 0.1
"""
self.demo_file='in.test'
with open(self.demo_file,'w') as f:
f.write(self.demo_input)
self.cont_file='in.cont'
with open(self.cont_file,'w') as f:
f.write(self.cont_input)
# clean up temporary files
def tearDown(self):
if os.path.exists(self.demo_file):
os.remove(self.demo_file)
if os.path.exists(self.cont_file):
os.remove(self.cont_file)
##############################
def testFile(self):
"""Test reading commands from a file"""
natoms = int(self.lmp.get_natoms())
self.assertEqual(natoms,0)
self.lmp.file(self.demo_file)
natoms = int(self.lmp.get_natoms())
self.assertEqual(natoms,1)
self.lmp.file(self.cont_file)
natoms = int(self.lmp.get_natoms())
self.assertEqual(natoms,2)
def testNoFile(self):
"""Test (not) reading commands from no file"""
self.lmp.file(None)
natoms = int(self.lmp.get_natoms())
self.assertEqual(natoms,0)
def testCommand(self):
"""Test executing individual commands"""
natoms = int(self.lmp.get_natoms())
self.assertEqual(natoms,0)
cmds = self.demo_input.splitlines()
for cmd in cmds:
self.lmp.command(cmd)
natoms = int(self.lmp.get_natoms())
self.assertEqual(natoms,1)
def testCommandsList(self):
"""Test executing commands from list of strings"""
natoms = int(self.lmp.get_natoms())
self.assertEqual(natoms,0)
cmds = self.demo_input.splitlines()+self.cont_input.splitlines()
self.lmp.commands_list(cmds)
natoms = int(self.lmp.get_natoms())
self.assertEqual(natoms,2)
def testCommandsString(self):
"""Test executing block of commands from string"""
natoms = int(self.lmp.get_natoms())
self.assertEqual(natoms,0)
self.lmp.commands_string(self.demo_input+self.cont_input)
natoms = int(self.lmp.get_natoms())
self.assertEqual(natoms,2)
##############################
if __name__ == "__main__":
unittest.main()

View File

@ -0,0 +1,57 @@
import sys,os,unittest
from lammps import lammps
has_mpi=False
has_mpi4py=False
try:
from mpi4py import __version__ as mpi4py_version
# tested to work with mpi4py versions 2 and 3
has_mpi4py = mpi4py_version.split('.')[0] in ['2','3']
except:
pass
try:
lmp = lammps()
has_mpi = lmp.has_mpi_support
lmp.close()
except:
pass
class PythonOpen(unittest.TestCase):
def setUp(self):
self.machine=None
if 'LAMMPS_MACHINE_NAME' in os.environ:
self.machine=os.environ['LAMMPS_MACHINE_NAME']
def testNoArgs(self):
"""Create LAMMPS instance without any arguments"""
lmp=lammps(name=self.machine)
self.assertIsNot(lmp.lmp,None)
self.assertEqual(lmp.opened,1)
self.assertEqual(has_mpi4py,lmp.has_mpi4py)
self.assertEqual(has_mpi,lmp.has_mpi_support)
lmp.close()
self.assertIsNone(lmp.lmp,None)
self.assertEqual(lmp.opened,0)
def testWithArgs(self):
"""Create LAMMPS instance with a few arguments"""
lmp=lammps(name=self.machine,
cmdargs=['-nocite','-sf','opt','-log','none'])
self.assertIsNot(lmp.lmp,None)
self.assertEqual(lmp.opened,1)
@unittest.skipIf(not (has_mpi and has_mpi4py),"Skipping MPI test since LAMMPS is not parallel or mpi4py is not found")
def testWithMPI(self):
from mpi4py import MPI
mycomm=MPI.Comm.Split(MPI.COMM_WORLD, 0, 1)
lmp=lammps(name=self.machine,comm=mycomm)
self.assertIsNot(lmp.lmp,None)
self.assertEqual(lmp.opened,1)
lmp.close()
if __name__ == "__main__":
unittest.main()