Refactor and add fix python/invoke tests
This commit is contained in:
@ -24,14 +24,14 @@
|
|||||||
#include <vector>
|
#include <vector>
|
||||||
#include <functional>
|
#include <functional>
|
||||||
|
|
||||||
|
#include "../testing/core.h"
|
||||||
|
#include "../testing/systems/melt.h"
|
||||||
|
|
||||||
// location of '*.py' files required by tests
|
// location of '*.py' files required by tests
|
||||||
#define STRINGIFY(val) XSTR(val)
|
#define STRINGIFY(val) XSTR(val)
|
||||||
#define XSTR(val) #val
|
#define XSTR(val) #val
|
||||||
std::string INPUT_FOLDER = STRINGIFY(TEST_INPUT_FOLDER);
|
std::string INPUT_FOLDER = STRINGIFY(TEST_INPUT_FOLDER);
|
||||||
|
|
||||||
// whether to print verbose output (i.e. not capturing LAMMPS screen output).
|
|
||||||
bool verbose = false;
|
|
||||||
|
|
||||||
const char * LOREM_IPSUM = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Praesent metus.";
|
const char * LOREM_IPSUM = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Praesent metus.";
|
||||||
|
|
||||||
using LAMMPS_NS::utils::split_words;
|
using LAMMPS_NS::utils::split_words;
|
||||||
@ -42,51 +42,12 @@ using ::testing::StrEq;
|
|||||||
using ::testing::Eq;
|
using ::testing::Eq;
|
||||||
using ::testing::HasSubstr;
|
using ::testing::HasSubstr;
|
||||||
|
|
||||||
class PythonPackageTest : public ::testing::Test {
|
class PythonPackageTest : public LAMMPSTest {
|
||||||
protected:
|
protected:
|
||||||
LAMMPS *lmp;
|
void InitSystem() override
|
||||||
Info *info;
|
|
||||||
|
|
||||||
void command(const std::string &line) { lmp->input->one(line.c_str()); }
|
|
||||||
|
|
||||||
void command_string(const std::string &lines) { lammps_commands_string(lmp, lines.c_str()); }
|
|
||||||
|
|
||||||
void HIDE_OUTPUT(std::function<void()> f) {
|
|
||||||
if (!verbose) ::testing::internal::CaptureStdout();
|
|
||||||
f();
|
|
||||||
if (!verbose) ::testing::internal::GetCapturedStdout();
|
|
||||||
}
|
|
||||||
|
|
||||||
std::string CAPTURE_OUTPUT(std::function<void()> f) {
|
|
||||||
::testing::internal::CaptureStdout();
|
|
||||||
f();
|
|
||||||
auto output = ::testing::internal::GetCapturedStdout();
|
|
||||||
if (verbose) std::cout << output;
|
|
||||||
return output;
|
|
||||||
}
|
|
||||||
|
|
||||||
double get_variable_value(const std::string & name) {
|
|
||||||
char * str = utils::strdup(fmt::format("v_{}", name));
|
|
||||||
double value = lmp->input->variable->compute_equal(str);
|
|
||||||
delete [] str;
|
|
||||||
return value;
|
|
||||||
}
|
|
||||||
|
|
||||||
std::string get_variable_string(const std::string & name) {
|
|
||||||
return lmp->input->variable->retrieve(name.c_str());
|
|
||||||
}
|
|
||||||
|
|
||||||
void SetUp() override
|
|
||||||
{
|
{
|
||||||
const char *args[] = {"PythonPackageTest", "-log", "none", "-echo", "screen", "-nocite"};
|
|
||||||
char **argv = (char **)args;
|
|
||||||
int argc = sizeof(args) / sizeof(char *);
|
|
||||||
HIDE_OUTPUT([&] {
|
|
||||||
lmp = new LAMMPS(argc, argv, MPI_COMM_WORLD);
|
|
||||||
});
|
|
||||||
ASSERT_NE(lmp, nullptr);
|
|
||||||
info = new Info(lmp);
|
|
||||||
if (!info->has_package("PYTHON")) GTEST_SKIP();
|
if (!info->has_package("PYTHON")) GTEST_SKIP();
|
||||||
|
|
||||||
HIDE_OUTPUT([&] {
|
HIDE_OUTPUT([&] {
|
||||||
command("units real");
|
command("units real");
|
||||||
command("dimension 3");
|
command("dimension 3");
|
||||||
@ -100,15 +61,15 @@ protected:
|
|||||||
command("variable input_dir index " + INPUT_FOLDER);
|
command("variable input_dir index " + INPUT_FOLDER);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
};
|
||||||
|
|
||||||
void TearDown() override
|
class FixPythonInvokeTest : public MeltTest {
|
||||||
|
protected:
|
||||||
|
void InitSystem() override
|
||||||
{
|
{
|
||||||
HIDE_OUTPUT([&] {
|
if (!info->has_package("PYTHON")) GTEST_SKIP();
|
||||||
delete info;
|
|
||||||
delete lmp;
|
MeltTest::InitSystem();
|
||||||
info = nullptr;
|
|
||||||
lmp = nullptr;
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -309,6 +270,56 @@ TEST_F(PythonPackageTest, RunSourceInline)
|
|||||||
ASSERT_THAT(output, HasSubstr("4"));
|
ASSERT_THAT(output, HasSubstr("4"));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
TEST_F(FixPythonInvokeTest, end_of_step)
|
||||||
|
{
|
||||||
|
HIDE_OUTPUT([&] {
|
||||||
|
command("python end_of_step_callback here \"\"\"\n"
|
||||||
|
"from __future__ import print_function\n"
|
||||||
|
"def end_of_step_callback(ptr):\n"
|
||||||
|
" print(\"PYTHON_END_OF_STEP\")\n"
|
||||||
|
"\"\"\"");
|
||||||
|
command("fix eos all python/invoke 10 end_of_step end_of_step_callback");
|
||||||
|
});
|
||||||
|
|
||||||
|
auto output = CAPTURE_OUTPUT([&] {
|
||||||
|
command("run 50");
|
||||||
|
});
|
||||||
|
|
||||||
|
auto lines = utils::split_lines(output);
|
||||||
|
int count = 0;
|
||||||
|
|
||||||
|
for(auto & line : lines) {
|
||||||
|
if (line == "PYTHON_END_OF_STEP") ++count;
|
||||||
|
}
|
||||||
|
|
||||||
|
ASSERT_EQ(count, 5);
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST_F(FixPythonInvokeTest, post_force)
|
||||||
|
{
|
||||||
|
HIDE_OUTPUT([&] {
|
||||||
|
command("python post_force_callback here \"\"\"\n"
|
||||||
|
"from __future__ import print_function\n"
|
||||||
|
"def post_force_callback(ptr, vflag):\n"
|
||||||
|
" print(\"PYTHON_POST_FORCE\")\n"
|
||||||
|
"\"\"\"");
|
||||||
|
command("fix pf all python/invoke 10 post_force post_force_callback");
|
||||||
|
});
|
||||||
|
|
||||||
|
auto output = CAPTURE_OUTPUT([&] {
|
||||||
|
command("run 50");
|
||||||
|
});
|
||||||
|
|
||||||
|
auto lines = utils::split_lines(output);
|
||||||
|
int count = 0;
|
||||||
|
|
||||||
|
for(auto & line : lines) {
|
||||||
|
if (line == "PYTHON_POST_FORCE") ++count;
|
||||||
|
}
|
||||||
|
|
||||||
|
ASSERT_EQ(count, 5);
|
||||||
|
}
|
||||||
|
|
||||||
} // namespace LAMMPS_NS
|
} // namespace LAMMPS_NS
|
||||||
|
|
||||||
int main(int argc, char **argv)
|
int main(int argc, char **argv)
|
||||||
|
|||||||
@ -16,9 +16,12 @@
|
|||||||
#include "info.h"
|
#include "info.h"
|
||||||
#include "input.h"
|
#include "input.h"
|
||||||
#include "lammps.h"
|
#include "lammps.h"
|
||||||
|
#include "variable.h"
|
||||||
#include "gmock/gmock.h"
|
#include "gmock/gmock.h"
|
||||||
#include "gtest/gtest.h"
|
#include "gtest/gtest.h"
|
||||||
|
|
||||||
|
#include <functional>
|
||||||
|
|
||||||
using namespace LAMMPS_NS;
|
using namespace LAMMPS_NS;
|
||||||
|
|
||||||
using ::testing::MatchesRegex;
|
using ::testing::MatchesRegex;
|
||||||
@ -45,29 +48,58 @@ class LAMMPSTest : public ::testing::Test {
|
|||||||
public:
|
public:
|
||||||
void command(const std::string &line) { lmp->input->one(line.c_str()); }
|
void command(const std::string &line) { lmp->input->one(line.c_str()); }
|
||||||
|
|
||||||
|
void HIDE_OUTPUT(std::function<void()> f) {
|
||||||
|
if (!verbose) ::testing::internal::CaptureStdout();
|
||||||
|
f();
|
||||||
|
if (!verbose) ::testing::internal::GetCapturedStdout();
|
||||||
|
}
|
||||||
|
|
||||||
|
std::string CAPTURE_OUTPUT(std::function<void()> f) {
|
||||||
|
::testing::internal::CaptureStdout();
|
||||||
|
f();
|
||||||
|
auto output = ::testing::internal::GetCapturedStdout();
|
||||||
|
if (verbose) std::cout << output;
|
||||||
|
return output;
|
||||||
|
}
|
||||||
|
|
||||||
|
double get_variable_value(const std::string & name) {
|
||||||
|
char * str = utils::strdup(fmt::format("v_{}", name));
|
||||||
|
double value = lmp->input->variable->compute_equal(str);
|
||||||
|
delete [] str;
|
||||||
|
return value;
|
||||||
|
}
|
||||||
|
|
||||||
|
std::string get_variable_string(const std::string & name) {
|
||||||
|
return lmp->input->variable->retrieve(name.c_str());
|
||||||
|
}
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
const char *testbinary = "LAMMPSTest";
|
const char *testbinary = "LAMMPSTest";
|
||||||
LAMMPS *lmp;
|
LAMMPS *lmp;
|
||||||
|
Info *info;
|
||||||
|
|
||||||
void SetUp() override
|
void SetUp() override
|
||||||
{
|
{
|
||||||
const char *args[] = {testbinary, "-log", "none", "-echo", "screen", "-nocite"};
|
const char *args[] = {testbinary, "-log", "none", "-echo", "screen", "-nocite"};
|
||||||
char **argv = (char **)args;
|
char **argv = (char **)args;
|
||||||
int argc = sizeof(args) / sizeof(char *);
|
int argc = sizeof(args) / sizeof(char *);
|
||||||
if (!verbose) ::testing::internal::CaptureStdout();
|
HIDE_OUTPUT([&] {
|
||||||
lmp = new LAMMPS(argc, argv, MPI_COMM_WORLD);
|
lmp = new LAMMPS(argc, argv, MPI_COMM_WORLD);
|
||||||
|
info = new Info(lmp);
|
||||||
|
});
|
||||||
InitSystem();
|
InitSystem();
|
||||||
if (!verbose) ::testing::internal::GetCapturedStdout();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
virtual void InitSystem() {}
|
virtual void InitSystem() {}
|
||||||
|
|
||||||
void TearDown() override
|
void TearDown() override
|
||||||
{
|
{
|
||||||
if (!verbose) ::testing::internal::CaptureStdout();
|
HIDE_OUTPUT([&] {
|
||||||
delete lmp;
|
delete info;
|
||||||
lmp = nullptr;
|
delete lmp;
|
||||||
if (!verbose) ::testing::internal::GetCapturedStdout();
|
info = nullptr;
|
||||||
|
lmp = nullptr;
|
||||||
|
});
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
@ -19,23 +19,25 @@ class MeltTest : public LAMMPSTest {
|
|||||||
protected:
|
protected:
|
||||||
virtual void InitSystem() override
|
virtual void InitSystem() override
|
||||||
{
|
{
|
||||||
command("units lj");
|
HIDE_OUTPUT([&] {
|
||||||
command("atom_style atomic");
|
command("units lj");
|
||||||
command("atom_modify map yes");
|
command("atom_style atomic");
|
||||||
|
command("atom_modify map yes");
|
||||||
|
|
||||||
command("lattice fcc 0.8442");
|
command("lattice fcc 0.8442");
|
||||||
command("region box block 0 2 0 2 0 2");
|
command("region box block 0 2 0 2 0 2");
|
||||||
command("create_box 1 box");
|
command("create_box 1 box");
|
||||||
command("create_atoms 1 box");
|
command("create_atoms 1 box");
|
||||||
command("mass 1 1.0");
|
command("mass 1 1.0");
|
||||||
|
|
||||||
command("velocity all create 3.0 87287");
|
command("velocity all create 3.0 87287");
|
||||||
|
|
||||||
command("pair_style lj/cut 2.5");
|
command("pair_style lj/cut 2.5");
|
||||||
command("pair_coeff 1 1 1.0 1.0 2.5");
|
command("pair_coeff 1 1 1.0 1.0 2.5");
|
||||||
|
|
||||||
command("neighbor 0.3 bin");
|
command("neighbor 0.3 bin");
|
||||||
command("neigh_modify every 20 delay 0 check no");
|
command("neigh_modify every 20 delay 0 check no");
|
||||||
|
});
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user